Export or Print Outlook Emails to PDF

Question:

I am using PowerShell to loop through designated folders in Outlook and saving the attachments in a tree like structure. This works wonders, but now management has requested the email itself be saved as a PDF as well. I found the PrintOut method in object, but that prompts for a file name. I haven’t been able to figure out what to pass to it to have it automatically save to a specific filename. I looked on the MSDN page and it was a bit to high for my current level.

I am using the com object of outlook.application.

Short of saving all of the emails to a temp file and using a third party method is there parameters I can pass to PrintOut? Or another way to accomplish this?

Here is the base of the code to get the emails. I loop through $Emails

Answer:

Looks like there are no built-in methods, but if you’re willing to use third-party binary, wkhtmltopdf can be used.

  1. Get precompiled binary (use MinGW 32-bit for maximum compatibility).
  2. Install or extract installer with 7Zip and copy wkhtmltopdf.exe to your script directory. It has no external dependencies and can be redistributed with your script, so you don’t have to install PDF printer on all PCs.
  3. Use HTMLBody property of MailItem object in your script for PDF conversion.

Here is an example:

Please note, that I’ve no experience with Outlook and used MSDN to get relevant properties for object, so the code might need some tweaking.

Source:

Export or Print Outlook Emails to PDF by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply