Dear Experts,
I have been using the below VBA for years and suddenly stopped working. This VBA is used to convert the Excel sheet to a PDF format and then attach this to an email. Not sure if the issue is related to an update or new version of Microsoft (I am now using 365) but long story short my VBA no longer works and I am now getting the following error:
"Run-time error '-2147024894 (80070002)':
Cannot find this file. Verify the path and the file name are correct."
Once debugged, it highlights this part of the VBA ".Attachments.Add PdfFile"
I have been using the below VBA for years and suddenly stopped working. This VBA is used to convert the Excel sheet to a PDF format and then attach this to an email. Not sure if the issue is related to an update or new version of Microsoft (I am now using 365) but long story short my VBA no longer works and I am now getting the following error:
"Run-time error '-2147024894 (80070002)':
Cannot find this file. Verify the path and the file name are correct."
Once debugged, it highlights this part of the VBA ".Attachments.Add PdfFile"
VBA Code:
Sub pdf_email()
Dim IsCreated As Boolean
Dim i As Long
Dim PdfFile As String, Title As String
Dim OutlApp As Object
' Not sure for what the Title is
Title = Range("A1")
' Define PDF filename
PdfFile = ActiveWorkbook.FullName
i = InStrRev(PdfFile, ".")
If i > 1 Then PdfFile = Left(PdfFile, i - 1)
PdfFile = ActiveSheet.Name & ".pdf"
' Export activesheet as PDF
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
' Use already open Outlook if possible
On Error Resume Next
Set OutlApp = GetObject(, "Outlook.Application")
If Err Then
Set OutlApp = CreateObject("Outlook.Application")
IsCreated = True
End If
OutlApp.Visible = True
On Error GoTo 0
' Prepare e-mail with PDF attachment
With OutlApp.CreateItem(0)
' Prepare e-mail
.Subject = Title
.Display
.To = "info@outlook.com"
.htmlbody = "<p style='font-family:calibri;font-size:15'>Good morning,</font></p>" & _
"<p style='font-family:calibri;font-size:15'>Please find attached the report.</font></p>" & _
"<p style='font-family:calibri;font-size:15'>Kind regards,</font></p>" & .htmlbody
.Attachments.Add PdfFile
.Display
' Delete PDF file
Kill PdfFile
End With
End Sub