简体   繁体   English

PrintOut 代码适用于 PowerShell v 5.1,但不适用于 7.3

[英]PrintOut Code Works On PowerShell v 5.1, But Not On 7.3

I am trying to print parts of several docx files.我正在尝试打印几个 docx 文件的一部分。 The code below works in Windows PowerShell 5.1, but not in PowerShell 7.3.下面的代码适用于 Windows PowerShell 5.1,但不适用于 PowerShell 7.3。 I would like to understand why and help updating the code so it works in PowerShell 7.3.我想了解原因并帮助更新代码,以便它在 PowerShell 7.3 中工作。

$path = 'D:\SchoolWeb\DOCXtoPDF\test.docx' #Path to docx

$Word = New-Object -ComObject Word.Application #open MS Word app
$Word.Visible = $false #hide the MS Word app
$docx = $Word.Documents.Open($path) #Open the docx from the path into the open MS Word app

$pagesNum = $Word.ActiveDocument.ComputeStatistics([Microsoft.Office.Interop.Word.WdStatistic]::wdStatisticPages) #Get the number of pages in the docx
$max=$pagesNum-2 #Get the maximum pages because the last two pages are not important

$Missing    = [System.Reflection.Missing]::Value  # Default values
$BackGround = 0
$Append     = 0
$Range      = 4 #https://learn.microsoft.com/en-us/office/vba/api/word.wdprintoutrange
$OutputFileName = $Missing
$From       = $Missing # values I've tried "1"
$To     = $Missing # values I've tried $max
$Item       = 0
$Copies     = 1
#$Pages = $max # values I've tried 
if ($max -gt 1) { $Pages = "1-$max" } else { $Pages = "1" }
    
$Word.PrintOut([ref]$BackGround, [ref]$Append, [ref]$Range, [ref]$OutputFileName, [ref]$From, [ref]$To, [ref]$Item, [ref]$Copies, [ref]$Pages)

$docx.Close($false)
$Word.Quit()

In Powershell 7.3, I get a MethodException "Exception setting "PrintOut": Cannot convert the "1" value of type "string" to type "Object"."在 Powershell 7.3 中,我得到一个 MethodException“异常设置“PrintOut”:无法将“字符串”类型的“1”值转换为“对象”类型。”

I know this relates to the $Pages variable.我知道这与 $Pages 变量有关。

Here are some places I already looked:以下是我已经看过的一些地方:

Thank you for explaining why the code works in one PowerShell and not the other and helping me to correct the code so it works in PowerShell 7.3.感谢您解释为什么代码在一个 PowerShell 中工作而不在另一个中工作,并帮助我更正代码以使其在 PowerShell 7.3 中工作。

Not sure where it goes sideways...不确定它会横向移动到哪里...
I suspect the parameter types (not just pages ) are not correctly aligned their position).我怀疑参数类型(不仅仅是pages )没有正确对齐它们的位置)。
Anyways, to resolve your issue, I recommend you to use named parameters.无论如何,要解决您的问题,我建议您使用命名参数。 See this answer: How to call a complex COM method from PowerShell?看到这个答案: How to call a complex COM method from PowerShell?

$Word.GetType().InvokeMember('PrintOut', [System.Reflection.BindingFlags]::InvokeMethod,
    $null,  ## Binder
    $Word,  ## Target
    ([Object[]]1),  ## Args
    $null,  ## Modifiers
    $null,  ## Culture
    ([String[]]'Pages')  ## NamedParameters
)

or using the function in the referenced answer:或在参考答案中使用 function:

Invoke-NamedParameter $Word Printout @{ Pages = 1 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM