在旧机器上(Windows 2008),我有:
PS P:\> $psversiontable
Name Value
---- -----
CLRVersion 2.0.50727.3623
BuildVersion 6.0.6002.18111
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
使用以下脚本:
$document = "C:\\Temp\\test.docx"
$word = new-object -comobject word.application
$word.Visible = $false
$doc = $word.Documents.Open($document)
$extensionSize = 3;
if ($document.EndsWith("docx")) {
$extensionSize = 4;
}
$dest = $document.Substring(0, $document.Length - $extensionSize) + "pdf"
$saveAsPath = [ref] $dest
$formatPDF = [ref] 17
$doc.SaveAs($saveAsPath, $formatPDF)
$doc.Close([ref]$false)
$word.quit()
在新计算机上(Windows 2008 R2):
PS H:\> $psversiontable
Name Value
---- -----
CLRVersion 2.0.50727.5466
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
我必须将脚本更改为:
$document = "c:\\Temp\\test.docx"
$word = new-object -comobject word.application
$doc = $word.Documents.Open($document)
$extensionSize = 3;
if ($document.EndsWith("docx")) {
$extensionSize = 4;
}
$dest = $document.Substring(0, $document.Length - $extensionSize) + "pdf"
Add-Type -AssemblyName "Microsoft.Office.Interop.Word"
$formats = "Microsoft.Office.Interop.Word.WdSaveFormat" -as [type]
$doc.SaveAs($dest, $formats::wdFormatPDF)
$doc.Close($false)
$word.quit()
任何人都可以解释脚本需要更改的原因吗?