Search code examples
excelvbaoutlook

Attachments.Add cannot find file


This code should fill form fields pulled from an Excel workbook on a PDF document, save it and then email the saved file to a specified address.

I sent multiple emails with the correct attachment.

Today the attachment fails with

Runtime error: "Cannot find this file, verify the file name and path are correct."

LastName = .Range("E" & CustRow).Value
FirstName = .Range("D" & CustRow).Value
PayoutDate = .Range("H" & CustRow).Value
TotalPayout = .Range("G" & CustRow).Value
ClientName = .Range("C" & CustRow).Value
Email = .Range("I" & CustRow).Value
ID = .Range("A" & CustRow).Value
CustNum = .Range("B" & CustRow).Value
AmountName = .Range("J" & CustRow).Value
Today = Format(Now(), "yyyymmdd")

Body = "<p>&nbsp;</p>" & _
"<p>We greatly appreciate your support as an ambassador of Briggs &amp; Riley. We&#39;re pleased to inform you that your monthly earnings from Briggs &amp; Riley&#39;s Spife program have been successfully processed, and a direct deposit payment will soon be made to your account. Your continued outstanding performance in driving Briggs &amp; Riley sales is truly valued. Here&#39;s to another fantastic month!</p>" & _
"<p>&nbsp;</p>" & _
"<p>Your dedicated Briggs &amp; Riley Team</p>"

NewPDFName = CustNum & LastName & Today & ".pdf"
SavePDFFolder = Worksheets("Create and Send").Range("F10").Value

Application.SendKeys "{Tab}", True
Application.SendKeys PayoutDate, True
Application.SendKeys "{Tab}", True
Application.SendKeys TotalPayout, True
Application.SendKeys "{Tab}", True
Application.SendKeys FirstName & " " & LastName, True
Application.SendKeys "{Tab}", True
Application.SendKeys AmountName, True

Application.SendKeys "^+(s)", True
Application.Wait Now + 0.00002
Application.SendKeys NewPDFName, True
Application.Wait Now + 0.00003
Application.SendKeys "%(s)", True
Application.Wait Now + 0.00003

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
    .To = Email
    .Subject = "Spife Payout"
    .HTMLBody = "Hello" & " " & FirstName & " " & LastName & Body
    .Attachments.Add NewPDFName
    .DeferredDeliveryTime = DateAdd("n", 10, Now)
    .SentOnBehalfOfName = "[email protected]"
    .Send

Solution

  • It does not look like NewPDFName contains a fully qualified file name (both the path and name). Make sure it does. e.g.,

    NewPDFName = "c:\temp\" CustNum & LastName & Today & ".pdf"