cancel
Showing results for 
Search instead for 
Did you mean: 

Outlook Email as a PDF File

VidhyaAnbalagan
Level 5

Hello BP Community,

is there any custom code available to save outlook email as a PDF file ?

I did create a custom code but it did not look like when we do print and save as a PDF manually.

Looking forward the solutions.

#EmailasPDF#Outlook#msgtopdf #emailconvertToPdf

Thanks,

Vidhya.

Support My Idea

6 REPLIES 6

SimonCooke1
Level 3

Hi Vidhya,

I have previously used the MS Interoperability objects to create a Word doc from the HTML code in the email and then convert to PDF from there. In C#

In my global code I have the following methods:

private static void ConvertMsgToPdf(string exportFilePath, string HtmlString)
{
HtmlString = TrimHtmlString(HtmlString);
// Define params
// Console.WriteLine("Define Params");
string MSWordExportFilePath = exportFilePath;
string HtmlFilePath = exportFilePath.Replace(".pdf", ".html");
WdExportFormat MSWordExportFormat = WdExportFormat.wdExportFormatPDF;
bool MSWordOpenAfterExport = false;
WdExportOptimizeFor MSWordExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
WdExportRange MSWordExportRange = WdExportRange.wdExportAllDocument;
Int32 MSWordStartPage = 0;
Int32 MSWordEndPage = 0;
WdExportItem MSWordExportItem = WdExportItem.wdExportDocumentContent;
bool MSWordIncludeDocProps = true;
bool MSWordKeepIRM = true;
WdExportCreateBookmarks MSWordCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool MSWordDocStructureTags = true;
bool MSWordBitmapMissingFonts = true;
bool MSWordUseISO19005_1 = false;
// Write Temp HTML File
// Console.WriteLine("Write Temp HTML File");
File.WriteAllText(HtmlFilePath, HtmlString);
// Convert HTML File to PDF
// Console.WriteLine("Convert HTML File to PDF");
Microsoft.Office.Interop.Word.Application wApp = new Microsoft.Office.Interop.Word.Application();
Document wdoc = new Document();
wdoc = wApp.Documents.Open(HtmlFilePath);
try
    {
        PageSetup pageSetup = wdoc.PageSetup;
        pageSetup.PageWidth = 1000;
    }
    catch {; }
wdoc.ExportAsFixedFormat(MSWordExportFilePath, MSWordExportFormat, MSWordOpenAfterExport, MSWordExportOptimizeFor, MSWordExportRange, MSWordStartPage, MSWordEndPage, MSWordExportItem, MSWordIncludeDocProps, MSWordKeepIRM, MSWordCreateBookmarks, MSWordDocStructureTags, MSWordBitmapMissingFonts, MSWordUseISO19005_1);
wdoc.Close();
wApp.Quit();
wdoc = null;
wApp = null;
// Console.WriteLine("Delete HTML File");
File.Delete(HtmlFilePath);
string tempHtmlDirectory = Path.GetDirectoryName(HtmlFilePath);
    string tempHtmlFileName = Path.GetFileName(HtmlFilePath).Replace(".html", "");
    string tempFolder = Path.Combine(tempHtmlDirectory, tempHtmlFileName + "_files");
    if (Directory.Exists(tempFolder))
    {
        Directory.Delete(tempFolder, true);
    }
}

private static string TrimHtmlString(string htmlString)
{
    string xml = "<?xml";
    string html = "<html";
    string head = "<head";
    string title = "<title";
    string style = "<style";
    string body = "<body";
    bool hasXml = htmlString.Contains(xml);
    bool hasHtml = htmlString.Contains(html);
    bool hasHead = htmlString.Contains(head);
    bool hasTitle = htmlString.Contains(title);
    bool hasStyle = htmlString.Contains(style);
    bool hasBody = htmlString.Contains(body);
    int indexXml = htmlString.IndexOf(xml);
    int indexHtml = htmlString.IndexOf(html);
    int indexHead = htmlString.IndexOf(head);
    int indexTitle = htmlString.IndexOf(title);
    int indexStyle = htmlString.IndexOf(style);
    int indexBody = htmlString.IndexOf(body);
    string htmlSubString = string.Empty;

    if (hasXml)
    {
        if (indexHtml == -1) { indexHtml = 999999999; }
        if (indexHead == -1) { indexHead = 999999999; }
        if (indexTitle == -1) { indexTitle = 999999999; }
        if (indexStyle == -1) { indexStyle = 999999999; }
        if (indexBody == -1) { indexBody = 999999999; }

        int firstIndex = indexHtml;
        if (indexHead < firstIndex) { firstIndex = indexHead; }
        if (indexTitle < firstIndex) { firstIndex = indexTitle; }
        if (indexStyle < firstIndex) { firstIndex = indexStyle; }
        if (indexBody < firstIndex) { firstIndex = indexBody; }

        htmlSubString = htmlString.Substring(firstIndex, htmlString.Length - firstIndex);
    }
    else
    {
        htmlSubString = htmlString;
    }
    return htmlSubString;
}

I call this from the page code stage like this:
35811.png35812.png35813.png
The following code options need to be added:
35814.png

VidhyaAnbalagan
Level 5

Thanks @SimonCooke1! I will try this. Really Appreciate your help!

PedroSL
Level 3

Hi Vidhya 

I also have the same need (save email as PDF file).

Do the solution proposed by Simon worked for you ? 

What is the difference between Simon implementation and just

  • reading the email body(HTML) with Get Received Items   
  • writing the HTML body to a file 
  • Reading it with Word and Saving it after as a PDF 

I know images will be lost but besides that ?  Does Simon solution preserves images ? 

VidhyaAnbalagan
Level 5

Hi Pedro Sobrado Lorenzo,

I didn't get a chance to try Simon Cooke code.

But I've developed a code to convert as pdf but the look & feel is not match when do print on email.

My version of code will convert the .msg to .pdf like below,

35818.png
if you would like to have the code snippet, Please let me know.
Thanks,
Vidhya.

sajid_sayyed
Level 2

Hello Vidhya,

Could you please provide your code snippet? or any other better solutions you founded for email to PDF conversion.

Thanks,

Sajid 

-------------------------- Sajid Sayyed Automation Consultant --------------------------

sonuiiml
Level 5

Hi Team, 

I have the same question too. I tried to incorporate the code as it was given by Simon Cooke. However, I am not able to arrange the office Microsoft.Office.Interop.Word.dll file. Could you please help me with this ?