Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save K4zuki/b6be0948b3fd8b247b6161e4155fbd98 to your computer and use it in GitHub Desktop.
Save K4zuki/b6be0948b3fd8b247b6161e4155fbd98 to your computer and use it in GitHub Desktop.
docx2pdf.ps1
$word = NEW-OBJECT -COMOBJECT WORD.APPLICATION
Write-Host "[Generate PDFs from DOCX's in current directory]"
$files = Get-ChildItem | Where-Object{ $_.Name -match "docx$" }
Write-Host "[Start process]"
foreach ($file in $files)
{
try
{
$result = (Test-Path $file.FullName.Replace(".docx", ".pdf"))
if ($result)
{
Write-Host "$( $file.Name ) ... file exists. Skipping"
}
else
{
Write-Host "$( $file.Name ) ... processing"
$doc = $word.Documents.OpenNoRepairDialog($file.FullName)
$doc.SaveAs([ref] $file.FullName.Replace(".docx", ".pdf"), [ref]17)
$doc.Close()
Write-Host "$($file.FullName.Replace(".docx", ".pdf") ) ... converted"
}
}
catch
{
Write-Host "[ERROR]$( $file.Name ) ... conversion failed"
}
}
Write-Host "[Process ended]"
$word.Quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment