Hi All,
I need to convert .msg to .pdf with out using ui activities/third party ,hence i used the vb code but it’s not saving the file into .pdf.
Kindly help me to resolve the issue.
below is the code i’m using
Dim objOutlook As microsoft.Office.Interop.Outlook.Application
Dim objMsg As microsoft.Office.Interop.Outlook._MailItem
Dim objWord As microsoft.Office.Interop.Word.Application
Dim objWordDoc As microsoft.Office.Interop.Word.Document
Dim objOutlookDoc As microsoft.Office.Interop.Word.Document
Console.WriteLine(“Test”)
objOutlook = New microsoft.Office.Interop.Outlook.Application
Console.WriteLine(“Test1”)
objMsg = CType(objOutlook.CreateItemFromTemplate(FilePath), _MailItem)
objMsg.SaveAs(SavePath)
Console.WriteLine(“Test2”)
FilePath is the path of .msg file
SavePath is the filelocation to save .pdf
I’m getting below error

This script worked for me:
Sub SaveMsgAsPDF()
Dim objItem As Object
Dim objMail As Outlook.MailItem
Dim objInspector As Outlook.Inspector
Dim objDocument As Word.Document
Dim objExportFormat As WdExportFormat
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim strFilePath As String
Dim olApp As Outlook.Application
'Change the file path to the desired location
strFilePath = "C:\Users.…\Temp\msg"
Set olApp = CreateObject(“Outlook.Application”)
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFolder = objFSO.GetFolder(strFilePath)
For Each objFile In objFolder.Files
If Right(objFile.Name, 4) = “.msg” Then
Set objMail = olApp.Session.OpenSharedItem(objFile.Path)
Set objInspector = objMail.GetInspector
Set objDocument = objInspector.WordEditor
objDocument.ExportAsFixedFormat OutputFileName:=strFilePath & Left(objFile.Name, Len(objFile.Name) - 4) & “.pdf”, ExportFormat:=wdExportFormatPDF
objMail.Close olDiscard
End If
Next
Set objItem = Nothing
Set objMail = Nothing
Set objInspector = Nothing
Set objDocument = Nothing
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Set olApp = Nothing
End Sub
Hi I’m getting an error,Please see the below image.
