且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Ms Access发送带有报告的电子邮件作为附件

更新时间:2022-12-01 12:01:45

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

As mentioned, export your report into an external file such as .pdf in order to attach to your outgoing email. Remember a report is an internal Access object and not readily in a file format for email. With DoCmd.OutputTo, you can dynamically create the pdf on the fly date-stamped and in same location as the database for a generalizeable solution for all your users.

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"