PowerShell

PowerShell: Convert Word documents to PDF documents (Bulk)

I often have to convert Microsoft word documents to pdf. What if you have really many documents to convert, let’s say 1000 or more? Would you open each documents seperately and click on save? Bad idea. In this blog post I will give you a PowerShell function at hand which enables you to convert multiple documents with a single command. This procedure is called automation ;-). Let’s dive in.

The Objective

As already mentioned we want to convert multiple Microsoft Word documents to pdf. My cmdlet ConvertWordTo-PDF does exactly that.

Cool. By the way, you can also omit the destination parameter. Pdf files will then be saved in the source folder.

Download

Klick on this link to download the PowerShell module.

The Code

function ConvertWordTo-PDF {

<# 
 
.SYNOPSIS 

ConvertTo-PDF converts Microsoft Word documents to PDF files. 
 
.DESCRIPTION 

The cmdlet queries the given source folder including sub-folders to find *.docx and *.doc files, 
converts all found files and saves them as pdf in the Destination folder. After completition, the Destination
folder with the newly created PDF files will be opened with Windows Explorer.
 
.PARAMETER SourceFolder
 
Mandatory. Enter the source folder of your Microsoft Word documents.
 
.PARAMETER DestinationFolder

Optional. Enter the Destination folder to save the created PDF documents. If you omit this parameter, pdf files will
be saved in the Source Folder.

.EXAMPLE 

ConvertWordTo-PDF -SourceFolder C:\Temp -DestinationFolder C:\Temp1
ConvertWordTo-PDF -SourceFolder C:\temp
 
.NOTES 
Author: Patrick Gruenauer | Microsoft PowerShell MVP [2018-2021] 
Web: https://sid-500.com 
 
#>

[CmdletBinding()]

param
(
 
[Parameter (Mandatory=$true,Position=0)]
[String]
$SourceFolder,
 
[Parameter (Position=1)]
[String]
$DestinationFolder = $SourceFolder

)

    $i = 0

    $word = New-Object -ComObject word.application 
    $FormatPDF = 17
    $word.visible = $false 
    $types = '*.docx','*.doc'

    If ((Test-Path $SourceFolder) -eq $false) {
    
    throw "Error. Source Folder $SourceFolder not found." } 

    If ((Test-Path $DestinationFolder) -eq $false) {
    
    throw "Error. Destination Folder $DestinationFolder not found." } 
    
    $files = Get-ChildItem -Path $SourceFolder -Include $Types -Recurse -ErrorAction Stop
    ''
    Write-Warning "Converting Files to PDF ..."
    ''
    
    foreach ($f in $files) {

        $path = $DestinationFolder + '\' + $f.Name.Substring(0,($f.Name.LastIndexOf('.')))
        $doc = $word.documents.open($f.FullName) 
        $doc.saveas($path,$FormatPDF) 
        $doc.close()
        Write-Output "$($f.Name)"
        $i++

    }
    ''
    Write-Output "$i file(s) converted."
    Start-Sleep -Seconds 2 
    Invoke-Item $DestinationFolder
    $word.Quit()
    
    
}





How to implement ConvertWordTo-PDF

Copy the code above into PowerShell ISE (ise.exe) or an editor of your choosing and run the code. Then type the command and have fun with it.

If you want to make the function permanently available, so that the function is available every time you start PowerShell, you have to create a folder in C:\Program Files\WindowsPowerShell\Modules. Name the folder ConvertWordTo-PDF. Then copy the psm1 file in that folder.

Credits

Credit goes to the author of the link below that inspired me and brought up the idea in me to create that function.

https://www.der-windows-papst.de/2018/05/17/powershell-word-convert-to-pdf-doc-docx-pdf/powershell-convert-word-to-pdf/

See also

Categories: PowerShell

Tagged as: ,

6 replies »

  1. Hello,
    When i run the script it disappears and not working for me, i am running it on windows 10, and i just ran another script for remove duplicates by you and worked perfectly.
    please advise how to get this script working?

    Like

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.