Bulk-Creating PDF Files from Word

by Jan 6, 2012

To convert a whole folder full of MS Word documents to PDF, here’s a function that might help:/p>

function Export-WordToPDF {
  param(
  [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
  [Alias("FullName")]
  $path, 
  $pdfpath = $null)

  process {
    if (!$pdfpath) {
      $pdfpath = [System.IO.Path]::ChangeExtension($path, '.pdf')
    }
    $word = New-Object -ComObject Word.Application
    $word.displayAlerts = $false
    
    $word.Visible = $true
    $doc = $word.Documents.Open($path)
    #$doc.TrackRevisions = $false
    $null = $word.ActiveDocument.ExportAsFixedFormat($pdfpath, 17, $false, 1)
    $word.ActiveDocument.Close()
    $word.Quit()
  }
} 

Use it like this:

PS> Dir c:\folder -Filter *.doc | Export-WordToPDF 

Twitter This Tip!
ReTweet this Tip!