Access发送电子邮件,其中包含报告作为附件

Sta*_*acy 3 email ms-access vba attachment report

在MS Access中使用VBA代码构建器,我已经能够编写打开Outlook的代码,并通过单击按钮向我发送电子邮件.我在添加附件时遇到问题.我发现的大多数代码都将MS数据库外的文件添加为附件,我想在我的数据库中添加一个作为附件创建的报告.

Private Sub EmailReport_Click()
Dim oApp As New Outlook.Application
Dim oEmail As Outlook.MailItem

'Email the results of the report generated
Set oEmail = oApp.CreateItem(olMailItem)
oEmail.To = "myemailaddress@email.com"
oEmail.Subject = "Training Roster"
oEmail.Body = "Roster Information"

With oEmail
    .Send
    MsgBox "Email Sent"
End With
Run Code Online (Sandbox Code Playgroud)

我一直在寻找类似的命令

oEmail.Attachments.Add Me.
Run Code Online (Sandbox Code Playgroud)

..但是,我找不到添加报告的正确组合.谢谢!!

Par*_*ait 5

如上所述,将报告导出到外部文件(如.pdf)中,以便附加到外发电子邮件中.请记住,报告是内部Access对象,而不是电子邮件的文件格式.使用DoCmd.OutputTo,您可以动态创建日期戳记的pdf,并在数据库相同的位置创建pdf,以便为所有用户提供可扩展的解决方案.

Private Sub EmailReport_Click()
Dim oApp As New Outlook.Application
Dim oEmail As Outlook.MailItem
Dim fileName As string, todayDate As String    

'Export report in same folder as db with date stamp
todayDate = Format(Date, "MMDDYYYY")
fileName = Application.CurrentProject.Path & "\ReportName_" & todayDate & ".pdf"
DoCmd.OutputTo acReport, "ReportName", acFormatPDF, fileName, False

'Email the results of the report generated
Set oEmail = oApp.CreateItem(olMailItem)
With oEmail
    .Recipients.Add "myemailaddress@email.com"
    .Subject = "Training Roster"
    .Body = "Roster Information"
    .Attachments.Add fileName
    .Send        
End With

MsgBox "Email successfully sent!", vbInformation, "EMAIL STATUS"
Run Code Online (Sandbox Code Playgroud)