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.
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.
- Sub MSG_to_PDF()
- Dim objOL As Object, Msg As MailItem, ThisFile As String
- On Error GoTo 0
- 'Set our folder to where the 'MSG' files are held
- InPath = "C:\temp\msg_to_pdf"
- 'Look for *.msg files in our folder
- ThisFile = Dir(InPath & "\*.msg")
- If (ThisFile = "") Then ' no file found so exit
- Exit Sub
- End If
- 'Open up Outlook for our use
- Set objOL = CreateObject("Outlook.Application")
- 'Open up Word , which will do the actual conversion from MHT (MIME HTML) to PDF
- Dim wrdApp As Word.Application
- Dim wrdDoc As Word.Document
- Set wrdApp = CreateObject("Word.Application")
- 'wrdapp.visible (uncomment to see the Word instance!)
- 'Loop through our MSG files..
- Do While ThisFile <> ""
- 'Open our MSG file
- Set Msg = objOL.Session.OpenSharedItem(InPath & "\" & ThisFile)
- 'Sort oout file name plus the new extensions
- New_FileName = Left(ThisFile, Len(ThisFile) - 3)
- Mht_File = New_FileName + "mht"
- PDF_FILE = New_FileName + "PDF"
- 'Save our MSG file as 'MHT' format
- Msg.SaveAs InPath + "\" + Mht_File, 10 '10 = olMHTML
- 'Open the mht file in Word without Word being visible
- Set wrdDoc = wrdApp.Documents.Open(Filename:=InPath + "\" + Mht_File, Visible:=False)
- 'Save as pdf
- wrdDoc.ExportAsFixedFormat OutputFileName:= _
- InPath + "\" + PDF_FILE, ExportFormat:= _
- wdExportFormatPDF
- 'Close our Word DOC(the MHT file)
- wrdDoc.Close
- 'get next file...
- ThisFile = Dir()
- Loop
- Set objOL = Nothing
- Set Msg = Nothing
- Set wrdApp = Nothing
- Set wrdDoc = Nothing
- '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...
- Kill InPath + "\*.MHT"
- x = MsgBox("Conversion(s) done")
- End Sub