Powershell 3: Convert Word to PDF

0

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?

powershell
pdf-generation
powershell-3.0
word-2010
asked on Stack Overflow Nov 11, 2013 by inquisitive_one • edited Nov 11, 2013 by inquisitive_one

1 Answer

0

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?

answered on Stack Overflow Nov 23, 2013 by inquisitive_one • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0