[英]How can I access object variables deep in MS-Word's Object Model using Powershell?
I'm just starting to scratch the surface with the Word Object Model, and have been studying MSDN with is rich with examples in VB and C#. 我才刚刚开始使用Word对象模型来摸索表面,并且一直在研究MSDN,其中包含VB和C#中的示例。 Unfortunately, I'm working with Powershell 2.0, and have found no good examples that help me understand how to get at the data I need.
不幸的是,我正在使用Powershell 2.0,却找不到很好的例子来帮助我理解如何获取所需的数据。 Also, please forgive some of the terminology I use...it may not be accurate;
另外,请原谅我使用的一些术语...可能不准确; please let me know the proper terminology if I use an improper term so I can improve clarity in future questions.
如果我使用了不当的用语,请让我知道正确的用语,这样我可以在以后的问题中提高清晰度。 Consider this script:
考虑以下脚本:
$global:word = new-object -ComObject Word.Application
$word.Visible = $False
$testfile = "\\path\to\file\foo.doc"
$doc = $word.Documents.Open($testfile)
$hyperlinks = @($doc2.Hyperlinks)
$hyperlinks # console output
$word.Quit()
$doc
has lots of, for lack of a better term, metadata...things that I can assign to variables via $foo1 = $doc.Fullname
, $foo2 = $doc.HasPassword
etc. There are also metadata fields which contain additional LISTS of information -- I believe these are Runtime Callable Wrappers (RCWs) -- which have a value of System.__ComObject
. $doc
缺少很多更好的术语,元数据...我可以通过$foo1 = $doc.Fullname
, $foo2 = $doc.HasPassword
等分配给变量的$foo2 = $doc.HasPassword
。还有一些元数据字段,其中包含其他内容信息列表-我相信它们是运行时可调用包装(RCW)-其值为System.__ComObject
。 The code above pulls the list-of-links object, assigns it to an array called $hyperlinks
, and when printed to the console, yields a series of records like this, each record representing a hyperlink in foo.doc
. 上面的代码提取链接列表对象,将其分配给名为
$hyperlinks
的数组,并在打印到控制台时,产生一系列这样的记录,每个记录代表foo.doc
的超链接。
Application : Microsoft.Office.Interop.Word.ApplicationClass
Creator : 1297307460
Parent : Microsoft.Office.Interop.Word.DocumentClass
Name : javascript:TextPopup(this)
AddressOld : javascript:TextPopup(this)
Type : 0
Range : System.__ComObject
Shape :
SubAddressOld :
ExtraInfoRequired : False
Address : javascript:TextPopup(this)
SubAddress :
EmailSubject :
ScreenTip :
TextToDisplay : IVR-generated calls
Target :
Here's where I run into some problems. 这是我遇到的一些问题。 First, I cannot seem to access any of the RCW's within other RCW's as variables.
首先,我似乎无法访问其他RCW中的任何RCW作为变量。 I can dump data, but not assign it to a specific variable.
我可以转储数据,但不能将其分配给特定变量。 For instance, I have been unable to generate any console output that represents the detailed contents of
Range
inside a Hyperlink
record. 例如,我无法生成任何表示
Hyperlink
记录内Range
的详细内容的控制台输出。 I've tried 我试过了
$ranges = @($hyperlinks.Range) # no output displays
$ranges = @($doc.hyperlinks.Range) # no output displays
$ranges = @($doc.Range) # yields the following
MemberType : Method
OverloadDefinitions : {Microsoft.Office.Interop.Word.Range Range(System.Object&, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Start, System.Object&, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyTo
ken=b77a5c561934e089 End)}
TypeNameOfValue : System.Management.Automation.PSMethod
Value : Microsoft.Office.Interop.Word.Range Range(System.Object&, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Start, System.Object&, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyTok
en=b77a5c561934e089 End)
Name : Range
IsInstance : True
I can output lists of individual values in a System.__ComObject
by using foreach
to populate an array as such. 我可以使用
foreach
像这样填充数组,从而在System.__ComObject
输出单个值的列表。
$hyperlinks | %{ $_.address }
$hyperlinks | %{ $_.SubAddress }
$hyperlinks | %{ $_.TextToDisplay }
My problem lies in assigning these values to variables. 我的问题在于将这些值分配给变量。 In the grand scheme, I'm trying to generate a report that will output a CSV file containing
在宏伟计划中,我试图生成一个报告,该报告将输出包含以下内容的CSV文件:
$doc.FullName; $doc.Hyperlinks.Address; $doc.Hyperlinks.SubAddress; $doc.Hyperlinks.TextToDisplay;
and have been unable to assign values in the $hyperlinks
array to specific fields. 并且无法将
$hyperlinks
数组中的值分配给特定字段。 I would like to do something like this pseudocode: 我想做这样的伪代码:
$o01 = $doc.FullName
$o02 = $hyperlinks | %{ $_.address }
$o03 = $hyperlinks | %{ $_.SubAddress }
$o04 = $hyperlinks | %{ $_.TextToDisplay }
$out = $o01 + ";" + $o02 + ";" + $o03 + ";" + $o4
$ofl = "outPutFile.csv"
$out | Out-File $ofl -append
to generate 产生
foo1.doc;foo2.html;;"Foo"
foo1.doc;foo3.html;foo2.html;"Foo again"
foo1.doc;foo4.html;foo3.html;"More Foo"
foo2.doc;foo5.html;foo1.html;"Foo"
foo2.doc;foo6.html;foo2.html;"Foo again"
foo3.doc;foo7.html;"More Foo"
where $doc
is repeated in column 1 for each instance of address
, subaddress
and TextToDisplay" appearing in the
$hyperlinks` array associated with that document. Can someone point me in the right direction? 其中
TextToDisplay" appearing in the
与该文档关联TextToDisplay" appearing in the
$ hyperlinks`数组中的address
, subaddress
和TextToDisplay" appearing in the
每个实例的第1列中都重复了$doc
。有人可以向我指出正确的方向吗?
Instead of using separate variables, try creating single objects. 不要尝试使用单独的变量,而应尝试创建单个对象。 Assuming $docs contains an array of all document objects you want to process, try something like this:
假设$ docs包含要处理的所有文档对象的数组,请尝试如下操作:
$results = $docs | % {
$docName = $_.FullName
$_.Hyperlinks | % {
New-Object -TypeName PSObject @Property @{
DocName = $docName;
Address = $_.address;
SubAddress = $_.SubAddress;
TextToDisplay = $_.TextToDisplay
}
}
}
$results | ConvertTo-Csv -NoTypeInformation -Delimiter ";" | Out-File $ofl
I don't have Word installed on the system I'm on now, so the code may require some tweaking to get the correct property names in place. 我现在所在的系统上没有安装Word,因此代码可能需要进行一些调整才能获得正确的属性名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.