We help IT Professionals succeed at work.

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

87 Views
Last Modified: 2019-01-09
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
Comment
Watch Question

NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
Lisa

What's the error message?
Jim Dettman (EE MVE)President / Owner
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2017

Commented:
<< 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.
President / Owner
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2017
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

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