Trying to write an Outlook Macro that converts specific emails to PDFs

In my job, I need to convert the same four emails to pdfs every day, with specific names including the date. I need to use the Adobe PDFMaker addin in Microsoft Outlook to do this. I wrote a Macro to try to automate this, and I almost have it working. It properly calls the PDFMaker addin, but when it tries to save, I just get “An Error Occurred while saving the Adobe PDF File”. I’m not sure if that’s an issue with my code or just an issue trying to save, but it gives this error message every time.

An Error Occurred while saving the Adobe PDF File

I’ve included my macro below. The things in [brackets] are names I replaced for anonymity/security/privacy/etc, so ignore the errors they would cause. (I’ll also admit that I used chatGPT to help my write this code.)

Sub SaveEmailsAsPDFWithPDFMaker()

    ' Object variables
    Dim objMail As MailItem
    Dim pdfMaker As Object
    ' String variables
    Dim pdfFileName As String
    Dim savePath As String
    Dim fullSavePath As String
    Dim emailReceivedDate As String
    Dim emailSubject As String
    ' Integer variables
    Dim i As Integer
    Dim emailCount As Integer

    ' Define the save path
    savePath = "C:Users[me]Documentsmacro test"

    ' Get the PDFMaker object from COM add-ins
    On Error Resume Next
    Set pdfMaker = Application.COMAddIns.Item("PDFMOutlook.PDFMOutlook").Object
    On Error GoTo 0

    ' send message if PDFMaker is not available
    If pdfMaker Is Nothing Then
        MsgBox "PDFMaker add-in is not available.", vbCritical
        Exit Sub
    End If

    ' Get the count of selected emails
    emailCount = Application.ActiveExplorer.Selection.Count

    ' Loop through selected emails
    For i = 1 To emailCount
        If TypeOf Application.ActiveExplorer.Selection.Item(i) Is MailItem Then
            Set objMail = Application.ActiveExplorer.Selection.Item(i)

            ' Get the subject of the email
            emailSubject = objMail.Subject
            Debug.Print "Processing email: " & emailSubject

            ' Get the received date of the email in the desired format
            emailReceivedDate = Format(objMail.ReceivedTime, "m-d-yy")
            Debug.Print "Received date: " & emailReceivedDate

            ' Generate the file name based on the subject line
            If Left(emailSubject, 47) = "[Email 1 Subject]" Then
                pdfFileName = "[Email 1] " & emailReceivedDate & ".pdf"
            ElseIf Left(emailSubject, 14) = "[Email 2 Subject]" Then
                pdfFileName = "[Email 2] " & emailReceivedDate & ".pdf"
            ElseIf Left(emailSubject, 18) = "[Email 3 Subject]" Then
                pdfFileName = "[Email 3] " & emailReceivedDate & ".pdf"
            ElseIf Left(emailSubject, 17) = "[Email 4 Subject]" Then
                pdfFileName = "[Email 4] " & emailReceivedDate & ".pdf"
            Else
                MsgBox "Email '" & emailSubject & "' does not match expected subject formats.", vbExclamation
                GoTo ContinueLoop
            End If

            ' Convert email to PDF and save
            On Error Resume Next
            pdfMaker.CreatePDF savePath & pdfFileName
            
            'Error message if it fails to convert
            If Err.Number <> 0 Then
                MsgBox "Failed to convert email '" & emailSubject & "' to PDF. Error " & Err.Number & ": " & Err.Description, vbCritical
                Err.Clear
            End If
            On Error GoTo 0


ContinueLoop:
        End If
    Next i

    MsgBox "Selected emails have been processed.", vbInformation
End Sub


I did a lot of troubleshooting to get the code to this working point. I’m at a loss, frankly.

New contributor

Michael Yao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật