Link to home
Start Free TrialLog in
Avatar of Lisa Norton
Lisa Norton

asked on

Save Report as PDF and send as attachment through Outlook - Compile Error

Good Morning,

I would like to use the following code that I found here on Experts Exchange.  I have altered some for my needs.  I get a compile error if I attempt to use this as an On Click Event.  I have also copied and pasted this in a module to use it as a Private Function.  I am not familiar with functions and how to call them or if you can even call them from an on click event.  The compile error occurs at the "DIM appoutlook as new outlook application.  I am using Access 2013.  I appreciate any help with this.  

Private Function SendPDFReport()
'Created by Helen Feddema 17-Jan-2010
'Last modified 17-Jan-2010
On Error GoTo ErrorHandler
   Dim appOutlook As New Outlook.Application
   Dim itm As Outlook.MailItem
   Dim strFileName As String
   Dim rpt As Access.Report
   Dim strReport As String
   Dim strCurrentPath As String
   Dim strFileNameAndPath As String
   strCurrentPath = "C:\users\13618\desktop\pdfs & " \ ""
   strReport = "R_UnitCheckByEmp"
   strFileName = Me.TxtFileName.pdf
   strFileNameAndPath = strCurrentPath & strFileName
   'Output report to PDF in current path
   DoCmd.OutputTo objecttype:=acOutputReport, _
      objectname:=strReport, _
      outputformat:=acFormatPDF, _
      outputfile:=strFileNameAndPath, _
   'Create new mail message and attach text file to it
   Set itm = appOutlook.CreateItem(olMailItem)
   With itm
      .To = ""
      .Subject = "Daily report"
      .Body = "Your message"
      .Attachments.Add strFileNameAndPath
      'To edit before sending
      'To send automatically
   End With
   Exit Sub
   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
   Resume ErrorHandlerExit
End Function
Avatar of Norie


What's the error message?
<< I am using Access 2013. >>

 You need to set a reference to Outlook. With the code displayed, click on Tools on the menu bar and then references.

 Scroll down Until you see "Microsoft Outlook 15.0 Object Library"

 Check the box and then click "OK".

 Then click "Debug" and "Compile".   Your error should be gone.

Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lisa Norton


Setting the outlook reference corrected the compile error.  I am still getting an error on the "send" line.  I seems that has a problem with sending multiple emails.  I will close this question and open a new one regarding that error.  Thank you Jim so much for your help and suggestions.