Ric*_*ney 1 powershell vba ms-word
我有一个 Word (Office 2013) 文档,我需要将该文档的每一页拆分为单独的 PDF。因此,我使用 PowerShell 将其组合在一起。
$Word = New-Object -ComObject Word.Application
$Word.Visible = $true
$Doc = $Word.Documents.Open($SourceFile)
for ($pageNo = 1; $pageNo -le 50; $pageNo++)
{
$OutputFile = $OutputDirectory + "\MyFile_" + $pageNo + ".pdf"
$Doc.ExportAsFixedFormat($OutputFile, [Microsoft.Office.Interop.Word.WdExportFormat]::wdExportFormatPDF, $false, [Microsoft.Office.Interop.Word.WdExportOptimizeFor]::wdExportOptimizeForPrint, [Microsoft.Office.Interop.Word.WdExportRange]::wdExportFromTo, $pageNo, $pageNo, [Microsoft.Office.Interop.Word.WdExportItem]::wdExportDocumentContent, $false, $false, [Microsoft.Office.Interop.Word.WdExportCreateBookmarks]::wdExportCreateNoBookmarks, $false, $true, $false, $null)
}
$Doc.Close()
$Word.Quit()
Run Code Online (Sandbox Code Playgroud)
我已经做到了最后一个参数,它期望 this 是对此的引用。
[ref] System.Object FixedFormatExtClassPtr
Run Code Online (Sandbox Code Playgroud)
我尝试传入 $null, 0,每个都带或不带 [ref],但出现此错误:
参数:“15”应该是 System.Management.Automation.PSReference。使用[参考]。
关于我需要为最后一个参数传递什么有什么想法吗?或者,有更简单的方法来完成这项任务吗?
我刚刚意识到我做错了什么。对于最后一个参数,我需要使用 System.Type.Missing。
$Word = New-Object -ComObject Word.Application
$Word.Visible = $true
$Doc = $Word.Documents.Open($SourceFile)
$fixedFromatExtClassPtr = [System.Type]::Missing
for ($pageNo = 1; $pageNo -le 50; $pageNo++)
{
$OutputFile = $OutputDirectory + "\MyFile_" + $pageNo + ".pdf"
$Doc.ExportAsFixedFormat($OutputFile, [Microsoft.Office.Interop.Word.WdExportFormat]::wdExportFormatPDF, $false, [Microsoft.Office.Interop.Word.WdExportOptimizeFor]::wdExportOptimizeForPrint, [Microsoft.Office.Interop.Word.WdExportRange]::wdExportFromTo, $pageNo, $pageNo, [Microsoft.Office.Interop.Word.WdExportItem]::wdExportDocumentContent, $false, $false, [Microsoft.Office.Interop.Word.WdExportCreateBookmarks]::wdExportCreateNoBookmarks, $false, $true, $false, [ref]$fixedFromatExtClassPtr)
}
$Doc.Close()
$Word.Quit()
Run Code Online (Sandbox Code Playgroud)