Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export or Print Outlook Emails to PDF

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

$Outlook = New-Object -comobject outlook.application
$Connection = $Outlook.GetNamespace("MAPI")
#Prompt which folder to process
$Folder = $Connection.PickFolder()
$Outlook_Folder_Path = ($Folder.FullFolderPath).Split("\",4)[3]
$BaseFolder += $Outlook_Folder_Path + "\"
$Emails = $Folder.Items
like image 923
Unfundednut Avatar asked Mar 28 '17 19:03

Unfundednut


People also ask

How do I mass convert Outlook emails to PDF?

Open Adobe Acrobat and choose Combine Files into PDF from the Select a Task section. Then open Outlook and drag all the emails you want to convert into the Combined Files window. Once done, click Combine Files button to start converting them.

Can you export an email as a PDF?

To convert an email to a PDF, you'll first need to navigate to the Print dialog within the specific email you want to convert. The Print dialog is typically resented by a printer icon. Tap on the printer icon. Select Save As PDF or Export As PDF from the Print dialog box.

How do I save multiple Outlook emails with attachments as PDF?

In Outlook Explorer window, select the emails from your Inbox or folder. 2. From the 'Save As PDF' menu in Email to PDF ribbon, select 'Combine selected Emails into a PDF Portfolio'. You can also right-click the selected emails and select 'Save As PDF' > Combine selected emails into a PDF Portfolio'.


1 Answers

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:

# Get path to wkhtmltopdf.exe
$ExePath = Join-Path -Path (
    Split-Path -Path $Script:MyInvocation.MyCommand.Path
) -ChildPath 'wkhtmltopdf.exe'

# Set PDF path
$OutFile = Join-Path -Path 'c:\path\to\emails' -ChildPath ($Email.Subject + '.pdf')

# Convert HTML string to PDF file
$ret = $Email.HTMLBody | & $ExePath @('--quiet', '-', $OutFile) 2>&1

# Check for errors
if ($LASTEXITCODE) {
    Write-Error $ret
}

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.

like image 109
beatcracker Avatar answered Oct 05 '22 17:10

beatcracker