Ms Access 以报告附件形式发送电子邮件

Ms Access Send Email with Report as Attachment

使用 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

我一直在研究类似于

的命令
oEmail.Attachments.Add Me.

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

您可以通过电子邮件将报告导出为 PDF:

DoCmd.SendObject(ObjectType, ObjectName, OutputFormat, To, Cc, Bcc,Subject, MessageText, EditMessage, TemplateFile)

如前所述,将您的报告导出到外部文件(例如 .pdf),以便附加到您的外发电子邮件中。请记住,报告是一个内部 Access 对象,并且不容易采用电子邮件的文件格式。使用 DoCmd.OutputTo, you can dynamically create the pdf on the fly date-stamped and in same location 作为数据库,为您的所有用户提供通用解决方案。

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"

使用 DoCmd.SendObject 您需要更新 Outlook Programmatic Access 以关闭有关自动发送电子邮件的警告。就我而言,域管理员启用了此选项以进行更改。更多信息 here.