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.
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