Take the 2-minute tour ×

I am trying to use PowerShell to do a batch conversion of Word Docx to PDF - using a script found on this site: http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/24/weekend-scripter-convert-word-documents-to-pdf-files-with-powershell.aspx

# Acquire a list of DOCX files in a folder
$Files=GET-CHILDITEM "C:\docx2pdf\*.DOCX"

Foreach ($File in $Files) {
    # open a Word document, filename from the directory

    # Swap out DOCX with PDF in the Filename

    # Save this File as a PDF in Word 2010/2013
    $Doc.saveas([ref] $Name, [ref] 17)  

And I keep on getting this error and can't figure out why:

PS C:\docx2pdf> .\docx2pdf.ps1
Exception calling "SaveAs" with "16" argument(s): "Command failed"
At C:\docx2pdf\docx2pdf.ps1:13 char:13
+     $Doc.saveas <<<< ([ref] $Name, [ref] 17)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Any ideas?

Also - how would I need to change it to also convert doc (not docX) files, as well as use the local files (files in same location as the script location)?

Sorry - never done PowerShell scripting...

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

This will work for doc as well as docx files.

$documents_path = 'c:\doc2pdf'

$word_app = New-Object -ComObject Word.Application

# This filter will find .doc as well as .docx documents
Get-ChildItem -Path $documents_path -Filter *.doc? | ForEach-Object {

    $document = $word_app.Documents.Open($_.FullName)

    $pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf"

    $document.SaveAs([ref] $pdf_filename, [ref] 17)


share|improve this answer
This worked - thanks - changed $documents_path to be dynamic based on script location by using $documents_path = Split-Path -parent $MyInvocation.MyCommand.Path –  takabanana May 14 '13 at 22:16
Is oledb used in this conversion? –  culter Oct 28 '13 at 14:37
add comment

This works for me (Word 2007):

$wdFormatPDF = 17
$word = New-Object -ComObject Word.Application
$word.visible = $false

$folderpath = Split-Path -parent $MyInvocation.MyCommand.Path

Get-ChildItem -path $folderpath -recurse -include "*.doc" | % {
    $path =  ($_.fullname).substring(0,($_.FullName).lastindexOf("."))
    $doc = $word.documents.open($_.fullname)
    $doc.saveas($path, $wdFormatPDF) 

share|improve this answer
add comment

Your Answer


