0

I'm using Word to convert a docx to PDF from PowerShell by opening the document and writing it by using SaveAs().

My code:

# got that hint from http://stackoverflow.com/questions/36487507/troubles-using-powershell-and-word-saveas
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Word") | Out-Null

$Word = New-Object -ComObject "Word.Application"
$Word.Visible = $False
foreach ($transf_file in $doc_path) {
  $transf_pdf_file = $transf_file -replace "`.docx?$", ".pdf"
  $rel_path = Split-Path -Parent $transf_file
  Copy-Item -Path "$src_pp_dir\$transf_file" -Destination "$dest_pp_dir\$rel_path" -Force
  $word_doc = $Word.Documents.Open( "$dest_pp_dir\$transf_file" )
  # Hess / Herlet workaround die nächste Zeile einkommentieren, die übernächste Zeile auskommentieren
  #$word_doc.SaveAs( "$dest_pp_dir\$transf_pdf_file" ,17 ) 
  $word_doc.SaveAs( [ref] [system.object] "$dest_pp_dir\$transf_pdf_file" ,[ref]17)  
  $word_doc.Close() 
  Remove-Item -Force -Path "$dest_pp_dir\$transf_file" 
}
# this line avoids saving of normal.dot what is normally requested when another
# Word is open in parallel
# the rest is necessary to kill this word process (otherwise they sum up in the
# system and after a while it doesn't work anymore)
$Word.NormalTemplate.Saved = $true
$Word.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Word) > $null
Remove-Variable Word 

The code runs on my computer as expected. On a colleagues computer the line:

$word_doc.SaveAs( [ref] [system.object] "$dest_pp_dir\$transf_pdf_file" ,[ref]17) 

throws an error:

Argument: '1' should not be a System.Management.Automation.PSReference. Do not
use [ref].
At D:\DMG\NX_Projekte\handle\PP-Encode.ps1:1015 char:7
+ $word_doc.SaveAs( [ref] [system.object] "$dest_pp_dir\$transf_pdf_file" ,[ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : RefArgumentToNonRefParameterMsg

On his machine the line before (now as comment, without [ref]), works fine.

What I checked:

  • We are using identical versions of PowerShell, Word, Word COM object, .NET Framework.
  • I scanned the hints on this site and improved in particular exiting from Word (making sure that all Word processes are finished -- what's the case on my computer).
  • I couldn't find any hints to this specific problem neither here nor elsewhere.
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • The error message is telling you to not use `[ref]`. Why do you feel the need to do it anyway? – Ansgar Wiechers Oct 04 '16 at 12:32
  • Your problem looks a lot like http://stackoverflow.com/questions/12199692/word-application-comobject-errors-in-powershell. Does any of the answers help? I'd assume that the .NET version used by PowerShell is different on your and your colleague's system. Have a look at the output of `$psversiontable` (see [here](http://stackoverflow.com/questions/3344855/which-net-version-is-my-powershell-script-using)) – Dirk Vollmar Oct 05 '16 at 07:16
  • Hi together, thanks for the answers. – A. Binder Oct 06 '16 at 12:25
  • I'm using [ref] because it is necessary on my Computer (without [ref] I get an error. On my colleagues machine it is the opposite, without [ref] it works there, with it Comes to an error. – A. Binder Oct 06 '16 at 12:27
  • The output of $psversiontable is identical on both machines – A. Binder Oct 06 '16 at 12:28
  • The property --> Details of WINWORD.exe are identical The CLSID of Word.Application is identical The version of the Microsoft.NET Framework is identical I'll try the hint in 12199692 and give Feedback soon – A. Binder Oct 06 '16 at 12:47
  • I tried the Suggestion from Dirk concerning issue 12199692 but still the same result, the new code lines are: $save_name = [ref]"$dest_pp_dir\$transf_pdf_file" $save_opt = [ref] [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatPDF $word_doc.SaveAs( ([ref] $save_name).Value, ([ref]$save_opt).Value ) but still the error messge Argument: '1' should not be a System.Management.Automation.PSReference. Do not use [ref]. At D:\DMG\NX_Projekte\handle\util\PP-Encode.ps1:1015 char:7 + $word_doc.SaveAs( ([ref] $save_name).Value, ([ref]$save_opt).Value ) – A. Binder Oct 07 '16 at 12:24

0 Answers0