vba Ms Access 发送带有报告的电子邮件作为附件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33918482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ms Access Send Email with Report as Attachment
提问by Stacy
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.
在 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 = "[email protected]"
oEmail.Subject = "Training Roster"
oEmail.Body = "Roster Information"
With oEmail
.Send
MsgBox "Email Sent"
End With
I have been looking into a command similar to
我一直在研究类似的命令
oEmail.Attachments.Add Me.
..But, I cannot find the correct combination for adding my report. Thanks!!
..但是,我找不到添加报告的正确组合。谢谢!!
回答by Parfait
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 locationas the database for a generalizeable solution for all your users.
如前所述,将您的报告导出到外部文件(例如 .pdf)中,以便附加到您的外发电子邮件中。请记住,报告是一个内部 Access 对象,不容易采用电子邮件文件格式。使用DoCmd.OutputTo,您可以动态创建带有日期戳的动态 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 "[email protected]"
.Subject = "Training Roster"
.Body = "Roster Information"
.Attachments.Add fileName
.Send
End With
MsgBox "Email successfully sent!", vbInformation, "EMAIL STATUS"
回答by David
You can export your report as PDF by email with this:
您可以通过电子邮件将报告导出为 PDF:
DoCmd.SendObject(ObjectType, ObjectName, OutputFormat, To, Cc, Bcc,Subject, MessageText, EditMessage, TemplateFile)
回答by user3627268
With using DoCmd.SendObjectyou need to update Outlook Programmatic Access to turn off warnings about auto sending emails. In my case, administrator of domain enabled this options to change. More information here.
使用时,DoCmd.SendObject您需要更新 Outlook Programmatic Access 以关闭有关自动发送电子邮件的警告。就我而言,域管理员启用了此选项以进行更改。更多信息在这里。

