IrishGuy725
New Member
- Joined
- Aug 10, 2023
- Messages
- 5
- Office Version
- 365
- Platform
- Windows
I have a macro code that is designed to save an excel sheet as a PDF AND email it out when you click the button. It still saves the sheet as a pdf and exports it to the correct folder. However, it is not emailing it out and I get an error message every time I try (error 287). This is the code:
Sub SavePdfAndSendEmail()
On Error GoTo err_handler
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
Dim folderPath As String
Dim pdfFileName As String
'// Construct the file path and name
folderPath = "XXX"
pdfFileName = Sheets("Info").Range("C2").Value & ".pdf"
'// Export the workbook as PDF
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:=folderPath & pdfFileName
'// Open Outlook and create a new email
Set oApp = New Outlook.Application
Set oMail = oApp.CreateItem(olMailItem)
With oMail
.To = "XXX"
.CC = ""
.Subject = "ASAP" & pdfFileName
.Body = "Please review this ASAP"
.Attachments.Add Source:=folderPath & pdfFileName, Type:=xlTypePDF
.Send
End With
clean_exit:
Set oMail = Nothing
Set oApp = Nothing
Exit Sub
err_handler:
'Something has gone wrong, spit out an error messsage
MsgBox Err.Number & ": " & Err.Description, vbCritical, "Error"
GoTo clean_exit
End Sub
I can't figure it out. Any help would be GREATLY appreciated
Sub SavePdfAndSendEmail()
On Error GoTo err_handler
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
Dim folderPath As String
Dim pdfFileName As String
'// Construct the file path and name
folderPath = "XXX"
pdfFileName = Sheets("Info").Range("C2").Value & ".pdf"
'// Export the workbook as PDF
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:=folderPath & pdfFileName
'// Open Outlook and create a new email
Set oApp = New Outlook.Application
Set oMail = oApp.CreateItem(olMailItem)
With oMail
.To = "XXX"
.CC = ""
.Subject = "ASAP" & pdfFileName
.Body = "Please review this ASAP"
.Attachments.Add Source:=folderPath & pdfFileName, Type:=xlTypePDF
.Send
End With
clean_exit:
Set oMail = Nothing
Set oApp = Nothing
Exit Sub
err_handler:
'Something has gone wrong, spit out an error messsage
MsgBox Err.Number & ": " & Err.Description, vbCritical, "Error"
GoTo clean_exit
End Sub
I can't figure it out. Any help would be GREATLY appreciated