简体   繁体   English

如何使用Powershell在MS-Word的对象模型中访问对象变量?

[英]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`数组中的addresssubaddressTextToDisplay" 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.

相关问题 如何使用.net中的.net库构建MS Word文件的在线文字处理器? - How can i build an Online word Processor using .net.. Libraries in .net for MS word files? 如何从其他应用程序(如MS Word或Visual Studio)访问和尝试解析剪贴板数据? - how can I access and try parse clipboard data from other applications, like MS Word or Visual Studio? 如何将对象传递给C#模型的变量 - How to pass an object to the variables of the c# model 将值从列表传递到MS-Word表 - Passing values from List to MS-Word table MS-Word标签通过c#.net优化问题 - Ms-Word Labeling through c#.net optimization issue 我需要通过使用动态访问另一个对象或 ArrayList 中的 JSON 对象。 如何使用动态访问 JSON 对象? - I need to access a JSON object inside another object or ArrayList via using dynamic. How can I access JSON objects using dynamic? 如何使用通过 a.Net PowerShell object 运行的 PowerShell 脚本获取 windows 注册表属性值? - How do I get windows registry property values using a PowerShell script run through a .Net PowerShell object? 如何访问使用 System.Test.Json 从字符串反序列化的动态 object 的属性? - How can I access properties of dynamic object deserialized from a string using System.Test.Json? 如何从Powershell访问.NET事件返回的对象args - How do I access the object args returned from a .NET event from Powershell 如何为PowerShell创建RunspaceConnectionInfo对象? - How do I Create a RunspaceConnectionInfo object for PowerShell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM