I am saving emails as PDF files. Most of the time the PDF file is created as a blank page.
If I add a Display
and Stop
and wait for the email to open completely (1-2 seconds), the PDF is created correctly.
Sometimes the macro will stop, no error, at some line. If I try to continue, nothing happens but if I click on a different window and then click back on the VBA editor, I can continue the macro.
When the PDF files are saved correctly, they are usually around 80-90kb. When the PDF files are saved incorrectly as a blank page, they are always 172kb or 173kb.
The email content is always a mix of tables, images, and text.
Sub save_email_as_pdf()
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder, destFolder As Outlook.Folder
Dim myItem As Object, objInspector As Object, objDoc As Object
Dim i As Long, strMatch As Long
Dim fullBody As String, sReference As String, filePath As String, fileName As String
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("VBAinbox")
Set destFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("VBAinbox").Folders("VBAcompleted")
'For Each myItem In myFolder.Items
For i = 1 To 10
Set myItem = myFolder.Items(i)
fullBody = myItem.Body
strMatch = InStr(myItem.Body, "Reference")
sConnote = Mid(fullBody, strMatch + 10, 9)
filePath = "C:\Users\myUserName\Documents\VBA\"
fileName = sReference
myItem.Display
Stop
Set objInspector = myItem.GetInspector 'if email is not loaded yet, macro will continue to save blank pdf
Set objDoc = objInspector.WordEditor
objDoc.exportasfixedformat OutputFileName:=filePath & fileName & ".pdf", ExportFormat:=17
myItem.Close False
myItem.Move destFolder
Set objInspector = Nothing
Set objDoc = Nothing
Next
End Sub