We help IT Professionals succeed at work.
Get Started

Attempting to Attach PDF file in Excel and Email but keep getting Error

Last Modified: 2016-03-11
I have the following VBA Code where I'm trying to attach a PDF file via Excel and everything runs fine until it gets to the .Attachments.Add line of code.

I'm getting the following error:

   Run-time error '-2147024894 (80070002)':
   Automation error
   The system cannot find the file specified.

But if I F8 (Step) through the code, the path/filename is correct.

Any clues?????



VBA Code:
Private Sub cmdEmailReport_Click()
Dim strPrevMnth As String
Dim strCurrMnth As String
Dim strPathName As String
Dim strRptName As String
Dim strEMRecipients As String
Dim strEMCCRecipients As String
Dim strEMSubject As String
Dim strEMBody As String
Dim strEMAttach As String

'Dates for Report Name
strPrevMnth = Format(DateSerial(Year(Date), Month(Now), 0), "MMM DD, YYYY")
strCurrMnth = Format(Now() - 1, "MMM DD, YYYY")

'Email Recipients
strEMRecipients = "gdunn59@abc.com"
strEMCCRecipients = "gdunn59@abc.com"

'Email Subject
strEMSubject = "Attorney Monthly Hours Report"

'Email Body
strEMBody = "Please find attached the Attorney Monthly Hours Report as of 3/8/2016."

'Path and Report Name
'strPathName = "H:\NRTEcho\ACTIVE\gdunn59\Atty Monthly Hours Report (WIP - Automate Email)\"
strPathName = "H:\NRTEcho\ACTIVE\gdunn59\"

strRptName = "Attorney Fiscal YTD Hrs Report Through " & strPrevMnth & " as of " & strCurrMnth & " - Full" & ".pdf"
'strRptName = "Attorney Fiscal YTD Hrs Report Through " & strPrevMnth & " as of " & strCurrMnth & " - Full"

strEMAttach = strPathName & strRptName

'Send Report via Email
Set Mail_Object = CreateObject("Outlook.Application")
Set Mail_Single = Mail_Object.CreateItem(0)
With Mail_Single
 .Subject = strEMSubject
 .To = strEMRecipients
 .cc = strEMCCRecipients
 .BCC = ""
 .Body = strEMBody
 .Attachments.Add (strEMAttach)
 End With
'If Err.Description "" Then MsgBox Err.Description
End Sub

Open in new window

Watch Question
Most Valuable Expert 2014
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE