Skip to content

Instantly share code, notes, and snippets.

@cl0ne
Created May 4, 2019 21:01
Show Gist options
  • Save cl0ne/3555a007bd24e5aa80dbc15b29c772fa to your computer and use it in GitHub Desktop.
Save cl0ne/3555a007bd24e5aa80dbc15b29c772fa to your computer and use it in GitHub Desktop.
Small PowerShell snippet for converting .doc(x) files to PDF, tested with Word 2010
function Word2PDF {
$src_path = $args[0]
if($src_path -eq $null){
Write-Host "Usage: Word2PDF source-files-dir"
return
}
if(!(Test-Path $src_path -PathType Container)){
throw "Path $src_path doesn't exist or is not a directory."
}
$word_app = New-Object -ComObject Word.Application
Get-ChildItem -Path $src_path -Filter *.doc? | ForEach-Object {
Write-Host "Converting $_"
$document = $word_app.Documents.Open($_.FullName)
$pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf"
$document.SaveAs([ref] $pdf_filename, [ref] 17)
$document.Close()
}
$word_app.Quit()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment