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

Show all categories only in K2 Joomla

php joomla joomla-k2

I want to show only category in my product page using k2, but when I choose from menu, it shows items from that category. how can I fix this ?

Django test case db giving inconsistent responses, caching or transaction culprit?

django unit-testing caching transactions

I am seeing some really surprising and frustrating behavior with Django testing. Model objects are being "found" by a related lookup, but no model objects exist. (I apologize for the weird descri...