When running the below code, it successfully creates a pdf file from the report, but I receive the following error when it attempts to attach the pdf file as an Outlook attachment:
"Can't find the file. Make sure the path and file name are correct".
Why can't it locate the file? The pdf file is there. I copied and pasted the path and file name from Windows 2000 Explorer to make certain it was correct.
Option Explicit
Public Function EmailPDFRpt()
'To create a PDF file from a MS Access report
'Uses Adobe Acrobat Professional 8
'Adobe PDF printer is assigned as default printer
Dim prnt As Printer
Dim strRptName As String
Dim strPDFPathNameMe As String
Dim objOutlookApp As Object
Dim objMailItem As Object
Set prnt = Application.Printers(0) 'Adobe pdf Printer (the default printer)
Set objOutlookApp = CreateObject("Outlook.Appl
ication") '(Set up Outlook as an object)
Set objMailItem = objOutlookApp.CreateItem(0
) '(An Outlook Mail Message)
strRptName = "rPDF-Email-Test" 'The MS Access Report's name
'The path and name of PDF file:
strPDFPathNameMe = "\\servername\users$\my.na
me\My Documents\PDF Files\rPDF-Email-Test.pdf"
'Closes the MS Access opening form "fOpenForm" so Adobe will not print it
DoCmd.Close acForm, "fOpenForm", acSaveNo
'Open and send report to Adobe PDF printer (the default priner)
DoCmd.OpenReport strRptName, acViewNormal, "", "", acHidden
DoCmd.Close acReport, strRptName
'Adobe will automatically give the pdf file the MS Access report name
'Email the pdf file via Outlook
Dim ol As Object
Dim itm As Object '(Creates the Outlook Application Object by accessing the MS Outlook COM Type Library)
Set ol = CreateObject("Outlook.Appl
ication")
Set itm = ol.CreateItem(0) '(Outlook a Mail Message)
itm.To = "cyndie.herbik@logan.edu"
itm.Subject = "Auto Reports - PDF ADM AUTO RPT"
itm.Body = "Attached IS A PDF FILE"
itm.Attachments.Add ""\\servername\users$\my.n
ame\My Documents\PDF Files\rPDF-Email-Test.pdf"
"
itm.Send '(to send)
Set itm = Nothing '(Cleans up)
Set ol = Nothing
End Function
Start Free Trial