Convert msg files (Outlook Emails) to pdf at specific folder

JBMS

New Member
Joined
Apr 9, 2021
Messages
7
Hi Team,

I am using the below code to Convert msg files (Outlook Emails) to pdf at specific folder. The code is converting text emails to PDF, however could not convert the emails which has tables and images/pictures in the body of the email. The entire message which is in body of the email is not captured in PDF and data is cropped.

Need your help for VBA code to convert emails which has tables and images/pictures in body of the email to PDF format.

  1. Sub MSG_to_PDF()

  2. Dim objOL As Object, Msg As MailItem, ThisFile As String

  3. On Error GoTo 0

  4. 'Set our folder to where the 'MSG' files are held
  5. InPath = "C:\temp\msg_to_pdf"

  6. 'Look for *.msg files in our folder
  7. ThisFile = Dir(InPath & "\*.msg")
  8. If (ThisFile = "") Then ' no file found so exit
  9. Exit Sub
  10. End If

  11. 'Open up Outlook for our use
  12. Set objOL = CreateObject("Outlook.Application")

  13. 'Open up Word , which will do the actual conversion from MHT (MIME HTML) to PDF
  14. Dim wrdApp As Word.Application
  15. Dim wrdDoc As Word.Document
  16. Set wrdApp = CreateObject("Word.Application")
  17. 'wrdapp.visible (uncomment to see the Word instance!)
  18. 'Loop through our MSG files..
  19. Do While ThisFile <> ""
  20. 'Open our MSG file
  21. Set Msg = objOL.Session.OpenSharedItem(InPath & "\" & ThisFile)
  22. 'Sort oout file name plus the new extensions
  23. New_FileName = Left(ThisFile, Len(ThisFile) - 3)
  24. Mht_File = New_FileName + "mht"
  25. PDF_FILE = New_FileName + "PDF"
  26. 'Save our MSG file as 'MHT' format
  27. Msg.SaveAs InPath + "\" + Mht_File, 10 '10 = olMHTML

  28. 'Open the mht file in Word without Word being visible
  29. Set wrdDoc = wrdApp.Documents.Open(Filename:=InPath + "\" + Mht_File, Visible:=False)

  30. 'Save as pdf
  31. wrdDoc.ExportAsFixedFormat OutputFileName:= _
  32. InPath + "\" + PDF_FILE, ExportFormat:= _
  33. wdExportFormatPDF
  34. 'Close our Word DOC(the MHT file)
  35. wrdDoc.Close
  36. 'get next file...
  37. ThisFile = Dir()

  38. Loop

  39. Set objOL = Nothing
  40. Set Msg = Nothing
  41. Set wrdApp = Nothing
  42. Set wrdDoc = Nothing

  43. 'Remove the MHT temporary files..- did try immediately after Word closes the MHT file, but would have to create time/pause, as Word takes a while...
  44. Kill InPath + "\*.MHT"

  45. x = MsgBox("Conversion(s) done")

  46. End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I haven't tested your code, but it seems that you could use the ExportAsFixedFormat method of the WordEditor object of the email, instead of the ExportAsFixedFormat method of Word's Document object. There would be no need to open the file in Word before exporting it to PDF. And it should be more efficient. So, for example, you could export your email message to PDF as follows . . .

VBA Code:
Msg.GetInspector.WordEditor.ExportAsFixedFormat OutputFileName:="c:\path\filename.pdf", ExportFormat:=17 'wdExportFormatPDF

Hope this helps!
 
Upvote 0
Hi Domenic,

Thank you for your kind response. I do not access to wordEditor application.

Request you to please help with other options.
 
Upvote 0
Actually, the WordEditor property of the Inspector object exists within the Outlook object library. It returns the Word Document object library of the message.

Have you tried it?
 
Upvote 0
Hi Domenic,

As suggested above, i have made changes to the code, however still facing the same issue. Could not convert the emails which has tables and images/pictures in the body of the email. The entire message which is in body of the email is not properly captured in PDF and data is cropped.

The code is below(The highlighted line was changed). Request you to please help.

  1. Sub MSG_to_PDF()

  2. Dim objOL As Object, Msg As MailItem, ThisFile As String

  3. On Error GoTo 0

  4. 'Set our folder to where the 'MSG' files are held
  5. InPath = "C:\temp\msg_to_pdf"

  6. 'Look for *.msg files in our folder
  7. ThisFile = Dir(InPath & "\*.msg")
  8. If (ThisFile = "") Then ' no file found so exit
  9. Exit Sub
  10. End If

  11. 'Open up Outlook for our use
  12. Set objOL = CreateObject("Outlook.Application")

  13. 'Open up Word , which will do the actual conversion from MHT (MIME HTML) to PDF
  14. Dim wrdApp As Word.Application
  15. Dim wrdDoc As Word.Document
  16. Set wrdApp = CreateObject("Word.Application")

  17. 'wrdapp.visible (uncomment to see the Word instance!)

  18. 'Loop through our MSG files..
  19. Do While ThisFile <> ""

  20. 'Open our MSG file
  21. Set Msg = objOL.Session.OpenSharedItem(InPath & "\" & ThisFile)

  22. 'Sort oout file name plus the new extensions
  23. New_FileName = Left(ThisFile, Len(ThisFile) - 3)
  24. Mht_File = New_FileName + "mht"
  25. PDF_FILE = New_FileName + "PDF"

  26. 'Save our MSG file as 'MHT' format
  27. Msg.SaveAs InPath + "\" + Mht_File, 10 '10 = olMHTML

  28. 'Open the mht file in Word without Word being visible
  29. Set wrdDoc = wrdApp.Documents.Open(Filename:=InPath + "\" + Mht_File, Visible:=False)

  30. 'Save as pdf
  31. Msg.GetInspector.WordEditor.ExportAsFixedFormat OutputFileName:="c:\path\filename.pdf", ExportFormat:=17 'wdExportFormatPDF
  32. InPath + "\" + PDF_FILE, ExportFormat:= _
  33. wdExportFormatPDF

  34. 'Close our Word DOC(the MHT file)
  35. wrdDoc.Close

  36. 'get next file...
  37. ThisFile = Dir()

  38. Loop

  39. Set objOL = Nothing
  40. Set Msg = Nothing
  41. Set wrdApp = Nothing
  42. Set wrdDoc = Nothing

  43. 'Remove the MHT temporary files..- did try immediately after Word closes the MHT file, but would have to create time/pause, as Word takes a while...
  44. Kill InPath + "\*.MHT"

  45. x = MsgBox("Conversion(s) done")

  46. End Sub
 
Upvote 0
Sorry, in that case, I don't know why it does not export to PDF properly. Hopefully someone else here will be able to help.

Cheers!
 
Upvote 0
Hi Team,

Need your help for the above requirement.
Hi Domenic,

As suggested above, i have made changes to the code, however still facing the same issue. Could not convert the emails which has tables and images/pictures in the body of the email. The entire message which is in body of the email is not properly captured in PDF and data is cropped.

The code is below(The highlighted line was changed). Request you to please help.

  1. Sub MSG_to_PDF()

  2. Dim objOL As Object, Msg As MailItem, ThisFile As String

  3. On Error GoTo 0

  4. 'Set our folder to where the 'MSG' files are held
  5. InPath = "C:\temp\msg_to_pdf"

  6. 'Look for *.msg files in our folder
  7. ThisFile = Dir(InPath & "\*.msg")
  8. If (ThisFile = "") Then ' no file found so exit
  9. Exit Sub
  10. End If

  11. 'Open up Outlook for our use
  12. Set objOL = CreateObject("Outlook.Application")

  13. 'Open up Word , which will do the actual conversion from MHT (MIME HTML) to PDF
  14. Dim wrdApp As Word.Application
  15. Dim wrdDoc As Word.Document
  16. Set wrdApp = CreateObject("Word.Application")

  17. 'wrdapp.visible (uncomment to see the Word instance!)

  18. 'Loop through our MSG files..
  19. Do While ThisFile <> ""

  20. 'Open our MSG file
  21. Set Msg = objOL.Session.OpenSharedItem(InPath & "\" & ThisFile)

  22. 'Sort oout file name plus the new extensions
  23. New_FileName = Left(ThisFile, Len(ThisFile) - 3)
  24. Mht_File = New_FileName + "mht"
  25. PDF_FILE = New_FileName + "PDF"

  26. 'Save our MSG file as 'MHT' format
  27. Msg.SaveAs InPath + "\" + Mht_File, 10 '10 = olMHTML

  28. 'Open the mht file in Word without Word being visible
  29. Set wrdDoc = wrdApp.Documents.Open(Filename:=InPath + "\" + Mht_File, Visible:=False)

  30. 'Save as pdf
  31. Msg.GetInspector.WordEditor.ExportAsFixedFormat OutputFileName:="c:\path\filename.pdf", ExportFormat:=17 'wdExportFormatPDF
  32. InPath + "\" + PDF_FILE, ExportFormat:= _
  33. wdExportFormatPDF

  34. 'Close our Word DOC(the MHT file)
  35. wrdDoc.Close

  36. 'get next file...
  37. ThisFile = Dir()

  38. Loop

  39. Set objOL = Nothing
  40. Set Msg = Nothing
  41. Set wrdApp = Nothing
  42. Set wrdDoc = Nothing

  43. 'Remove the MHT temporary files..- did try immediately after Word closes the MHT file, but would have to create time/pause, as Word takes a while...
  44. Kill InPath + "\*.MHT"

  45. x = MsgBox("Conversion(s) done")

  46. End Sub
Hi, wld like to know if anyone managed to resolve this? Images in .msg gets truncated when being converted to .pdf
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top