Click here to Skip to main content
14,268,557 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello coders,

So I have a problem with the conversion of MSG to PDF files. Here is my code currently:
If TypeOf objItem Is Outlook.MailItem Then
                Dim EmailMessages As String = "EmailMessages"
                If Not Directory.Exists(strDest + "\" + EmailMessages) Then
                    Directory.CreateDirectory(strDest + "\" + EmailMessages)
                End If

                'save it as MSG (native)
                MItem = CType(objItem, Outlook.MailItem)
                Dim rTime As String = Format(MItem.ReceivedTime, "dd.MMM.yyyy (hh.mm.ss tt)")

                MItem.SaveAs(strFolderPath & "\" + MItem.SenderName.ToString + " " + rTime + ".msg", Outlook.OlSaveAsType.olMSG)

                'save it as PDF
                MItem.SaveAs(strFolderPath & "\" + MItem.SenderName.ToString + " " + rTime + ".html", Outlook.OlSaveAsType.olHTML)
                strPathPDF = strFolderPath & "\" + MItem.SenderName.ToString + " " + rTime
                wordDoc = wordApp.Documents.Open(strPathPDF + ".html")
                wordDoc.ExportAsFixedFormat(strPathPDF + ".pdf", Word.WdExportFormat.wdExportFormatPDF)
                wordDoc.Close()
End If



MITEM is my placeholder for MailItems.

Problem is I want to do it directly from MSG to PDF. I understand this isn't possible with Ms Office alone. I have come across Foxit PDF creator which is essentially a printer driver which can print out PDF's to file. This is great an all but the Printout() method doesn't take any arguments so I am reduced to typing in the file name and choosing the directory and clicking save manually. I do not want to do this. Is there a way to get past this printing thing. Or better yet is there a way to do this without any 3rd party software? Any help will be appreciated :D

Regards,

Adeeb
Posted
Updated 12-Aug-19 21:47pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can have a look at PDFSharp - http://pdfsharp.codeplex.com/[^].

BTW, in below code why are you not saving directly to pdf instead of HTML format? Is there not an other option in the OLSaveAsType?

'save it as PDF
MItem.SaveAs(strFolderPath & "\" + MItem.SenderName.ToString + " " + rTime + ".html", Outlook.OlSaveAsType.olHTML)
   
Comments
chilipadiboy 14-Aug-12 23:52pm
   
Hi Prahlad,

Nope there is no direct way in Outlook at least to save as pdf. Also printing doesn't accept arguments so I cant use a pdf to file printer driver. I guess I could create a PDF but the metadata found on the MSG files will probably be lost.

Regards,

Adeeb
Rate this:
Please Sign up or sign in to vote.

Solution 9

chilipadiboy[^] wrote:
I have come across Foxit PDF creator which is essentially a printer driver which can print out PDF's to file. This is great an all but the Printout() method doesn't take any arguments (...)


Well, even if you can't pass parameters into PrintOut() method, you can print out email to Foxit PDF creator by using Process.Start() method[^]. Note: you'll need to save email as a *.doc or *.rtf file first! System will use the default application, which is associated to that file, to open it. So, the default printer should be set to Foxit PDF Creator.
Dim psi As New ProcessStartInfo
PrintPDF.UseShellExecute = True
PrintPDF.Verb = "print"
PrintPDF.WindowStyle = ProcessWindowStyle.Hidden
PrintPDF.FileName = sFullFileName 'file Name including path, name and extension
Process.Start(psi)


For further details about printing documents by using Process.Start method, please refer this: How to Silently Print PDFs using Adobe Reader and C#[^]


Another way is to print MailItem.Body[^] by using PrintDocument Class (System.Drawing.Printing) | Microsoft Docs[^]. Then you'll able to print that document directly to Foxit PDF Creator. Note that Body returns a string representation of clear-text body of the Outlook item. So, depending on MailItem.BodyFormat property (Outlook) | Microsoft Docs[^] you'll get one of three body text formats: Plain Text, Rich Text (RTF), and HTML.
   
Comments
Richard MacCutchan 13-Aug-19 4:25am
   
Did you notice the date of this question?
Maciej Los 13-Aug-19 4:47am
   
No... Thank you, Richard.
This question has no accepted answer, so i decided to leave my answer. I hope, that it is useful for some one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100