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.

1 REPLY 1

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