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

问题描述:

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

Using VBA code builder in MS Access, I have been able to write code that opens Outlook and send me an email with the click of a button. I am having problems with adding an attachment. Most code I have found adds files outside the MS Database as an attachment, I would like to add a report created in my database as an attachment.

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.

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

..But, I cannot find the correct combination for adding my report. Thanks!!

如上所述,请将您的报告导出到外部文件(例如.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"