cancel
Showing results for 
Search instead for 
Did you mean: 

Convert a .msg file to .pdf

sapna_soni0290
Level 5

Hi Team,

I have a .msg file which i want to convert to .msg file. I have learned that in order to convert a .msg to .pdf, u need to

1. Open .msg into outlook & save as .html

2. Open .html from word & do save as pdf.

I am looking for an action or vbo code that can do Step #1. Has anyone used a similar functionality earlier ?

PS - My input is .msg. I dont have that email in outlook , so dont have its entry id. Please suggest a way with above mentioned approach or any other approach that does not require spying.

Your help is much appreciated.

2 REPLIES 2

Try creating object with this code mentioned below


Library imports

Microsoft.Office.Interop.Outlook
System.IO
iTextSharp.text
iTextSharp.text.pdf

Code State Input Parameters - Action Page - MSG to PDF

Input Parameters:

  • msgFilePath: The file path of the .msg file you want to convert.
  • pdfFilePath: The file path where the output PDF should be saved.

Code Stage

' Create an Outlook application instance
Dim outlookApp As New Application()

' Open the .msg file
Dim mailItem As MailItem = CType(outlookApp.Session.OpenSharedItem(msgFilePath), MailItem)

' Convert the email body to HTML
Dim htmlBody As String = mailItem.HTMLBody

' Create a new PDF document
Dim pdfDoc As New Document(PageSize.A4)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream(pdfFilePath, FileMode.Create))

' Open the PDF document
pdfDoc.Open()

' Add the HTML content to the PDF document
Using sr As New StringReader(htmlBody)
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
End Using

' Close the PDF document
pdfDoc.Close()

' Release Outlook COM objects
System.Runtime.InteropServices.Marshal.ReleaseComObject(mailItem)
System.Runtime.InteropServices.Marshal.ReleaseComObject(outlookApp)

' Final clean-up
mailItem = Nothing
outlookApp = Nothing
GC.Collect()

Parthiban A

jamesrobert15
Level 2

To convert .MSG files to PDF, you can use DRS Softech MSG Converter Tool. This software allows you to convert multiple MSG files to PDF in bulk while maintaining the original formatting, metadata, and attachments. It provides options to preview the emails before conversion and offers advanced filters for selective file conversion. The tool ensures high-quality output without any data loss and is compatible with all Windows versions. With an easy-to-use interface, both technical and non-technical users can efficiently convert MSG files to PDF without hassle.