0

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

BigBen
  • 46,229
  • 7
  • 24
  • 40
  • Always helps to tell us which line has the error. Comment out the `On Error GoTo err_handler` and see where the problem lies. – Tim Williams Aug 10 '23 at 18:09
  • I don't think there is a definitive explanation. You could vote on the workarounds [Sending mail using Outlook where the Send method fails](https://stackoverflow.com/questions/17883088/sending-mail-using-outlook-where-the-send-method-fails) and [Send mail through Outlook - Error 287](https://stackoverflow.com/questions/39901116/send-mail-through-outlook-error-287) – niton Aug 26 '23 at 15:19

0 Answers0