我无法获取 Word 2010 (14.0.x) 文档SaveAs http://msdn.microsoft.com/en-us/LIBRary/microsoft.office.interop.word.documentclass.saveas%28v=office.11%29 or Close http://msdn.microsoft.com/en-us/LIBRary/microsoft.office.interop.word.documentclass.close%28v=office.11%29使用 Powershell。从网上的所有评论来看,它似乎应该与 2.0 一起工作,但我不再有这个了。
简单案例:
$Path = "C:\MyDoc.docx"
$Word = New-Object -comobject Word.Application
$Word.Visible = $True #Do this to close it out without task manager
$Doc = $Word.Documents.Open($Path)
$Doc.SaveAs($Path)
$Doc.Close()
此时一切正常,直到保存并关闭:
Argument: '1' should be a System.Management.Automation.PSReference. Use [ref].
At line:5 char:1
+ $Doc.SaveAs($Path)
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : NonRefArgumentToRefParameterMsg
Argument types do not match
At line:6 char:1
+ $Doc.Close()
+ ~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : Argument types do not match
似乎 Get-Member 显示为有参数的任何方法都会失败。例如,调用一个简单的 $Doc.Save() 似乎效果很好。查看有关这些方法的 MSDN 信息,看起来需要类似保存更改 http://msdn.microsoft.com/en-us/library/bb336792方法,但老实说,目前这超出了我的能力范围。
我尝试过传递 $Null 或 $True 或 $False 以希望获得幸运,但它只是一直对我犹豫不决。
我所能找到的只是它显然是链接至 PS 3.0 Beta http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/52d447fa-6c45-429e-a216-bf9a9e690670/(似乎在 2.0 中对人们来说工作得很好)和评论艾德·威尔逊还没有回来 http://blogs.technet.com/b/heyscriptingguy/archive/2012/06/14/use-powershell-to-add-autocorrect-entries-to-word.aspx.
我也为这个错误苦苦挣扎,但最终通过调用 PReference 的“Value”属性解决了这个问题(我在这里得到了我的信息:https://msdn.microsoft.com/en-us/library/system.management.automation.psreference(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/system.management.automation.psreference(v=vs.85).aspx )
这最终导致了代码行:
$filename = [ref]"C:\Temp\pv_report.docx"
[ref]$option = [Microsoft.Office.Interop.Word.WdSaveFormat] -as [type]
$document.SaveAs(([ref]$filename).Value, ([ref]$option::wdFormatDocumentDefault).Value)
$document.Close()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)