Powershell 3: Convert Word to PDF

I'm fairly new to Powershell and found the following script on the web: (I've modified it for my project)

# Acquire a list of DOC files in a folder
$Word = New-Object -ComObject word.application 
$formats = "Microsoft.Office.Interop.Word.WdSaveFormat" -as [type]
$Word.visible = $false
$fileTypes = "*.docx","*doc"
$complyDocPath = "i:\2014\COMPLY\DOC\"
$complyPDFPath = "i:\2014\COMPLY\PDF\"
$Files=GET-CHILDITEM $complyDocPath -include $fileTypes

Foreach ($File in $Files) {
    # open a Word document, filename from the directory
    #$Doc=$Word.Documents.Open($File.fullname) 
    $Doc=$Word.Documents.Open($File) 

    # Swap out .DOCX/.DOC with 5.PDF in the Filename
    $fileName = [system.io.path]::GetFileNameWithoutExtension($File)
    $destPath = Join-Path -path $complyPDFPath -childpath ($fileName +"5.PDF")
    Add-Type -AssemblyName "Microsoft.Office.Interop.Word"

    "Converting $File to pdf ... $destPath"

    # Save this File as a PDF in Word 2010/2013
    $Doc.SaveAs($destPath, $formats::wdFormatPDF)
    $Doc.Close($false)
}

$Word.Quit()

I get the following errors:

Argument: '2' should be a System.Management.Automation.PSReference. Use [ref].
At line:25 char:5
+     $Doc.SaveAs($destPath, $formats::wdFormatPDF)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : NonRefArgumentToRefParameterMsg

Argument: '1' should be a System.Management.Automation.PSReference. Use [ref].
At line:26 char:5
+     $Doc.Close($false)
+     ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : NonRefArgumentToRefParameterMsg

Exception calling "Open" with "1" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
At line:14 char:5
+     $Doc=$Word.Documents.Open($File)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

For the 1st error, I changed my code to include [ref], like these variations (but to no success):

$Doc.SaveAs([ref] $destPath, $formats::wdFormatPDF)
$Doc.SaveAs($destPath, [ref] $formats::wdFormatPDF)
$Doc.SaveAs([ref] $destPath, [ref] 17)

[Update] FYI: Files in the source directory are named like this:

I:\2014\Comply\DOC\IBM=US.DOC
I:\2014\Comply\DOC\CTC.A=CA.DOC

How can I generate a PDF from Word doc?

Answers


The above code works if the source & destination paths were the same. Since I didn't get any responses, I've decided to go the C#.Net route and implemented the code that I found in: How do I convert Word files to PDF programmatically?


Need Your Help

How to Calculate Recurring Digits?

algorithm language-agnostic division

Given two integers a and b, how would I go about calculating the repeating decimal of a / b? This can be in any language; whatever it's easiest for you to express it in.

Is it possible to efficiently look ahead more than one Char in Attoparsec?

regex parsing haskell attoparsec

I'm trying to augment Haskell's Attoparsec parser library with a function