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, _
      autostart:=False
   
   'Create new mail message and attach text file to it
   Set itm = appOutlook.CreateItem(olMailItem)
   With itm
      .To = "someone@xyz.com"
      .Subject = "Daily report"
      .Body = "Your message"
      .Attachments.Add strFileNameAndPath
      'To edit before sending
      .Display
      'To send automatically
      '.Send
   End With
   
ErrorHandlerExit:
   Exit Sub
 
ErrorHandler:
   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
   Resume ErrorHandlerExit
 
End Function
ExchangeOutlookMicrosoft Access

Avatar of undefined
Last Comment
Lisa Norton

8/22/2022 - Mon
Norie

Lisa

What's the error message?
Jim Dettman (EE MVE)

<< 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.

Jim.
ASKER CERTIFIED SOLUTION
Jim Dettman (EE MVE)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Lisa Norton

ASKER
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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23