Note that there are some explanatory texts on larger screens.

plurals
  1. POPowershell 3: Convert Word to PDF
    text
    copied!<p>I'm fairly new to Powershell and found the following script on the web: (I've modified it for my project)</p> <pre><code># 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() </code></pre> <p>I get the following errors:</p> <pre><code>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 </code></pre> <p>For the 1st error, I changed my code to include [ref], like these variations (but to no success):</p> <pre><code>$Doc.SaveAs([ref] $destPath, $formats::wdFormatPDF) $Doc.SaveAs($destPath, [ref] $formats::wdFormatPDF) $Doc.SaveAs([ref] $destPath, [ref] 17) </code></pre> <p>[Update] FYI: Files in the source directory are named like this:</p> <pre><code>I:\2014\Comply\DOC\IBM=US.DOC I:\2014\Comply\DOC\CTC.A=CA.DOC </code></pre> <p>How can I generate a PDF from Word doc?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload