Export Word to PDF using PowerShell

In some situations you may want to create a PDF or XPS file after releasing a Microsoft Word document, for instance to upload it to an ERP system or to archive the document.

The Microsoft Word API (Version 2007 and 2010) encloses a function in the Microsoft.Office.Interop.Word namespace for exporting PDF or XPS: ExportAsFixedFormat(). In this blog we show you how to invoke this function using PowerShell.

First of all we have to add the class that we want to use to our PowerShell session:

Add-type -AssemblyName Microsoft.Office.Interop.Word

After that we open a Word application instance:

$wdApplication = New-Object -ComObject "Word.Application"

open the document:

$wdDocument = $wdApplication.Documents.Open($wdSourceFile)

and export the document using some parameters:

$wdDocument.ExportAsFixedFormat(<parameters>)

Finally we have to close the document, quit the application and clean up our ressources:

if ($wdDocument)
{
       $wdDocument.Close()
       $wdDocument = $null
}
if ($wdApplication)
{
       $wdApplication.Quit()
       $wdApplication = $null
}
[GC]::Collect()
[GC]::WaitForPendingFinalizers()

A detailed description on the function and the parameters can be found on MSDN: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.exportasfixedformat.aspx

But even before you take a look at the documentation, you can use the following sample script to immediately convert your first Word document to PDF. All you have to do is to adjust the two file names in line 2 and 3… isn’t that cool?!

Add-type -AssemblyName Microsoft.Office.Interop.Word

$wdSourceFile = "C:\Users\christian\Desktop\Test.docx"
$wdExportFile = "C:\Users\christian\Desktop\Test.pdf"

$wdExportFormat = [Microsoft.Office.Interop.Word.WdExportFormat]::wdExportFormatPDF
$wdOpenAfterExport = $false
$wdExportOptimizeFor = [Microsoft.Office.Interop.Word.WdExportOptimizeFor]::wdExportOptimizeForOnScreen
$wdExportRange = [Microsoft.Office.Interop.Word.WdExportRange]::wdExportAllDocument
$wdStartPage = 0
$wdEndPage = 0
$wdExportItem = [Microsoft.Office.Interop.Word.WdExportItem]::wdExportDocumentContent
$wdIncludeDocProps = $true
$wdKeepIRM = $true
$wdCreateBookmarks = [Microsoft.Office.Interop.Word.WdExportCreateBookmarks]::wdExportCreateWordBookmarks
$wdDocStructureTags = $true
$wdBitmapMissingFonts = $true
$wdUseISO19005_1 = $false

$wdApplication = $null;
$wdDocument = $null;

try
{
       $wdApplication = New-Object -ComObject "Word.Application"
       $wdDocument = $wdApplication.Documents.Open($wdSourceFile)
       $wdDocument.ExportAsFixedFormat(
       $wdExportFile,
       $wdExportFormat,
       $wdOpenAfterExport,
       $wdExportOptimizeFor,
       $wdExportRange,
       $wdStartPage,
       $wdEndPage,
       $wdExportItem,
       $wdIncludeDocProps,
       $wdKeepIRM,
       $wdCreateBookmarks,
       $wdDocStructureTags,
       $wdBitmapMissingFonts,
       $wdUseISO19005_1
       )
}
catch
{
       $wshShell = New-Object -ComObject WScript.Shell
       $wshShell.Popup($_.Exception.ToString(), 0, "Error", 0)
       $wshShell = $null
}
finally
{
       if ($wdDocument)
       {
              $wdDocument.Close()
              $wdDocument = $null
       }
       if ($wdApplication)
       {
              $wdApplication.Quit()
              $wdApplication = $null
       }
       [GC]::Collect()
       [GC]::WaitForPendingFinalizers()
}

By the way, in conjunction with coolOrange powerJobs you can use this script to convert a Word document within Autodesk Vault into a PDF/XPS and add it back to Vault as an attachment to the original file.

CG

This entry was posted in PowerShell, Vault API and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s