Microsoft Word Interop: SaveAs hangs for some mist

2019-09-12 00:21发布

问题:

When trying to simplify a PowerShell script that converts from Word to PDF, I found out the following weird scenario that blows my mind. Here's the transcript from a proof of concept interactive session:

Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\Users\Frag> Add-Type -AssemblyName Microsoft.Office.Interop.Word
PS C:\Users\Frag> $word = New-Object -ComObject Word.Application
PS C:\Users\Frag> $i = Get-Item ".\document.docx"
PS C:\Users\Frag> $i.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     FileInfo                                 System.IO.FileSystemInfo


PS C:\Users\Frag> $i.FullName
C:\Users\Frag\document.docx
PS C:\Users\Frag> $doc = $word.Documents.Open($i.FullName)
PS C:\Users\Frag> $doc.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    DocumentClass                            System.__ComObject


PS C:\Users\Frag> $good = $i.FullName.Remove($i.FullName.Length - $i.Extension.Length) + ".pdf"
PS C:\Users\Frag> $bad = Join-Path $i.DirectoryName ($i.BaseName + ".pdf")
PS C:\Users\Frag> $good
C:\Users\Frag\document.pdf
PS C:\Users\Frag> $bad
C:\Users\Frag\document.pdf
PS C:\Users\Frag> $good -eq $bad
True
PS C:\Users\Frag> $good.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


PS C:\Users\Frag> $bad.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


PS C:\Users\Frag> Remove-Item -LiteralPath $good -Force -ErrorAction SilentlyContinue
PS C:\Users\Frag> $doc.SaveAs($good, [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatPDF)
PS C:\Users\Frag> dir $good


    Directory: C:\Users\Frag


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       11/03/2017     22:50         174616 document.pdf


PS C:\Users\Frag> Remove-Item -LiteralPath $good -Force
PS C:\Users\Frag> $doc.SaveAs($bad, [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatPDF)

The last command hangs, and the shell window has to be closed as well as the invisible Word instance killed from the task manager.

By the looks of it, $good and $bad appear to be of different string types. Does anyone know what is going on here?

Update

I managed to fix the issue by adding an explicit cast:

PS C:\Users\Frag> [string]$bad = Join-Path $i.DirectoryName ($i.BaseName + ".pdf")

So the issue is gone, but I'd still very much appreciate an explanation or a pointer.

回答1:

    $saveFormat = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatDocumentDefault
    $doc.saveas([ref][system.object]$wordFile, [ref]$saveFormat)

works for me...