如何从PowerShell将丢失的可选参数传递给VBA方法 [英] How to pass a missing optional parameter to a VBA method from PowerShell

查看:73
本文介绍了如何从PowerShell将丢失的可选参数传递给VBA方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Word(Office 2013)文档,我需要将文档的每一页拆分为一个单独的PDF.因此,我使用PowerShell将它们放在一起.

I have a Word (Office 2013) document and I need to split each page of the document into a separate PDF. So, I put this together using 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()

我已经达到了最后一个参数,它希望这是对此的引用.

I've made it as far as the last parameter, which is expecting this a reference to this.

[ref] System.Object FixedFormatExtClassPtr

我尝试传递$ null,0,每个都带有或不带有[ref],但出现此错误:

I've tried passing in $null, 0, each with or without [ref], but I get this error:

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

Argument: '15' should be a System.Management.Automation.PSReference. Use [ref].

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

Any ideas on what I need to pass in for this last parameter? Or, is there an easier way to achieve this task?

推荐答案

我只是想出自己做错了什么.对于最后一个参数,我需要使用System.Type.Missing.

I just figured out what I was doing wrong. For the last parameter, I needed to use 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()

这篇关于如何从PowerShell将丢失的可选参数传递给VBA方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆