如何从PowerShell向VBA方法传递缺少的可选参数

如何从PowerShell向VBA方法传递缺少的可选参数,vba,powershell,ms-word,Vba,Powershell,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 = $Outp

我有一个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()
我已经把它设置为最后一个参数,它希望这是对它的引用

[ref] System.Object FixedFormatExtClassPtr
我尝试传入$null,0,每个都有或没有[ref],但我得到了以下错误:

参数:“15”应该是System.Management.Automation.PSReference。 使用[ref]


关于最后一个参数需要传递什么,有什么想法吗?或者,有没有更简单的方法来完成这项任务?

我刚刚发现我做错了什么。对于最后一个参数,我需要使用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()

我刚刚发现我做错了什么。对于最后一个参数,我需要使用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()