Error trying to save Word document as PDF in PowerShell script

4

I have the following PowerShell script to convert a folder of docx files to pdf:

param (
    [string]$folder = '.'
)

Add-type -AssemblyName Microsoft.Office.Interop.Word

$word_app = New-Object -ComObject Word.Application
$word_app.visible = $false

Get-ChildItem -Path "$folder\Output" -Filter *.pdf | ForEach-Object {
    remove-item $_.FullName
}

Get-ChildItem -Path "$folder\Input" -Filter *.docx | ForEach-Object {
    $document = $word_app.Documents.Open($_.FullName)

    # Remove spaces in the name as we're uploading this to the web
    $filename = $_.BaseName -replace ' ',''

    $pdf_filename = "$($folder)\Output\$($filename).pdf"

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

    $document.Close()
}

$word_app.Quit()

This worked fine on Windows 7 (64 bit) with Office 2010 (32 bit).

I have since had a new machine with Windows 8.1 (64 bit) and Office 2013 (32 bit) installed and now when I try to run the script I get errors on the SaveAs line. The first error was that it said the [ref] modifier on the arguments wasn't necessary. So I changed that line to:

    $document.SaveAs($pdf_filename, 17)

However, this now errors with the following:

Exception calling "SaveAs" with "2" argument(s): "Command failed"
At D:\Documents\User Guides\ConvertToPDF.ps1:31 char:5
+     $document.SaveAs($pdf_filename, 17)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

There is no other indication of what's wrong. The output folder exists and is writeable. I've added code to traverse the error structure and the inner exception is:

Message: Command Failed
ErrorCode: 800A1066 (-2146824090)

Which would seem to indicate some sort of COM failure.

I've also tried

    $document.ExportAsFixedFormat($pdf_filename, 17)

but that gives me

Exception calling "ExportAsFixedFormat" with "2" argument(s): "The directory name is not valid."
At D:\Documents\User Guides\ConvertToPDF.ps1:33 char:5
+     $document.ExportAsFixedFormat($pdf_filename, 17)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

Despite the fact that the directory name is valid.

I've searched online, but all the pages I've found indicated that this is all I need to do.

The only other thing I've just thought of is that I might be using a higher version of PowerShell than before - though I don't really see how that would affect things.

What am I missing?

ChrisF

Posted 2015-06-26T09:20:14.400

Reputation: 39 650

Are you saving to a new file, or overwritting an existing file? Or does the issue persist either way? – Dave – 2015-06-26T09:48:51.140

@Dave - saving to a new file. If you look I delete any existing pdfs in the output folder first. – ChrisF – 2015-06-26T09:50:14.140

... I wonder if that is the problem. Yes you delete it, but, I wonder if the OS has a lock to the handle. Would you try giving it a totally unique name just to prove me wrong (hard code it in for testing)? I've had horrible problems removing files and saving 'over' them in this manner due to Anti-Virus's locking them for scans too... – Dave – 2015-06-26T09:54:13.257

@Dave - well in this case the files didn't exist as I'm converting a new set of docx's, so the output folder starts off as empty. – ChrisF – 2015-06-26T09:56:24.043

A shot in the dark here - is Word 32 or 64 bit? – Dave – 2015-06-26T10:08:04.013

Also, and sorry if you know this, there is an article here which gets 'more' error information out which may help http://blog.kickercost.com/2012_01_01_archive.html - See the section titled Powershell Exception trapping

– Dave – 2015-06-26T10:15:51.590

Answers

0

The problem was with the path to the pdf file.

I was specifying that the script look in the current folder by passing in "." so the output name was:

.\Output\Example.pdf

when I added the line:

$fullFolder = Resolve-Path -Path $folder

and then used $fullFolder instead of $folder the SaveAs worked as the output filename became:

D:\Documents\User Guides\Output\Example.pdf

I found this by trying to SaveAs a Word document in the same folder as the pdf to see if it was having problems accessing the folder. That came back with the error:

Exception calling "SaveAs" with "2" argument(s): "This is not a valid file name.
Try one or more of the following:
* Check the path to make sure it was typed correctly.
* Select a file from the list of files and folders."

This lead me down the track of checking the path as the actual filename could not be invalid as it was the same as the input filename.

ChrisF

Posted 2015-06-26T09:20:14.400

Reputation: 39 650