在下面的代码中,$range2.Paste($copy)
行无法在PS4及以下版本上运行。
错误:异常类型:System.Management.Automation.MethodInvocationException 异常消息:使用“1”参数调用“Paste”的异常:“对象引用未设置为对象的实例。”
最小代码:
$word = New-Object -ComObject word.application
$word.visible = $false
$word2 = New-Object -ComObject word.application
$word2.visible = $false
$doc = $word.documents.open($source)
$doc2 = $word2.documents.add($template)
$range = $doc.Range()
$copy=""
$copy = $range.Copy()
$range2 = $doc2.Range()
$range2.Paste($copy)
$doc2.saveas([ref]$destination,[ref]$saveFormat::wdFormatDocument)