PowerShell 中的 Word.Application ComObject 错误 [英] Word.Application ComObject errors in PowerShell

查看:71
本文介绍了PowerShell 中的 Word.Application ComObject 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将 Word 2010 (14.0.x) 文档发送到 SaveAs关闭 使用 Powershell.从网上的所有 tuts 来看,它似乎应该适用于 2.0,但我不再拥有它了.

I'm unable to get a Word 2010 (14.0.x) document to SaveAs or Close using Powershell. From all the tuts online it seems like it should be working with 2.0, but I don't have that anymore.

简单案例:

$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()

此时一切正常,直到保存和关闭:

At this point everything works up until the saving and closing:

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 信息,它看起来像 SaveChanges方法,但老实说,这超出了我目前的技能.

It seems like any methods that Get-Member shows as having arguments fail. For instance, calling a simple $Doc.Save() works fine, it seems. Looking at the MSDN info on those methods it looks like it takes things like the SaveChanges method, but that's honestly beyond my skills at this point.

我试过传递 $Null 或 $True 或 $False 以期获得好运,但它一直对我犹豫不决.

I've tried passing $Null or $True or $False in hopes of getting lucky, but it just keeps balking at me.

我能找到的只是它显然是 链接到 PS 3.0 Beta(似乎在 2.0 中对人们工作正常)和评论 埃德·威尔逊还没有回来.

All I've been able to find is that it is apparently linked to PS 3.0 Beta (seems to work fine in 2.0 for people) and a comment Ed Wilson hasn't gotten back to.

推荐答案

我也为这个错误苦苦挣扎,但最终通过调用 PSReference 的Value"属性解决了它(我在这里得到了我的信息:https://msdn.microsoft.com/en-us/library/system.management.automation.psreference(v=vs.85).aspx )

I was also struggling al lot with this error, but got finally around it by calling the "Value" property of the PSReference (I got my Information here: 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()

这篇关于PowerShell 中的 Word.Application ComObject 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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