Ms Access Send Email with Report as Attachment

后端 未结 3 614
傲寒
傲寒 2021-01-06 04:30

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

相关标签:
3条回答
  • 2021-01-06 04:43

    You can export your report as PDF by email with this:

    DoCmd.SendObject(ObjectType, ObjectName, OutputFormat, To, Cc, Bcc,Subject, MessageText, EditMessage, TemplateFile)
    
    0 讨论(0)
  • 2021-01-06 04:49

    With using DoCmd.SendObject you 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.

    0 讨论(0)
  • 2021-01-06 04:59

    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"
    
    0 讨论(0)
提交回复
热议问题