Word 2007 VBA:ActiveDocument.CustomXMLParts 和下拉列表
不确定将下拉列表内容控件正确绑定到 XML 文件的最佳方法:我得到的只是第一项。
我假设我必须遍历 XML 文档,计算项目数,然后相应地调用控件上的 .Add
方法,但我不确定如何在VBA。
这就是我所拥有的:
Dim ap As Document
Dim cnt As Integer
Set ap = ActiveDocument
cnt = ap.CustomXMLParts.Count + 1
ap.CustomXMLParts.Add
ap.CustomXMLParts(cnt).Load ("C:\test\Employees.xml")
Dim strXPath1 As String
strXPath1 = "/Employees/Employee/@name"
ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath1
哪个(如预期)获得名字属性;只是不确定如何最好地从 XML 文档填充内容控制下拉列表(请参阅下面的 XML 文档):
<?xml version="1.0"?>
<Employees>
<Employee name="Joe Blow">
<Email>[email protected]</Email>
<Extension>201</Extension>
</Employee>
<Employee name="Bob Smith">
<Email>[email protected]</Email>
<Extension>202</Extension>
</Employee>
</Employees>
Unsure of the best way to bind a a drop-down list Content Control to an XML file properly: all I'm getting is the first item.
I'm assuming I'll have to iterate through the XML document, count number of items, and then call the .Add
method on the control accordingly, but I'm not sure how to do that in VBA.
Here's what I have:
Dim ap As Document
Dim cnt As Integer
Set ap = ActiveDocument
cnt = ap.CustomXMLParts.Count + 1
ap.CustomXMLParts.Add
ap.CustomXMLParts(cnt).Load ("C:\test\Employees.xml")
Dim strXPath1 As String
strXPath1 = "/Employees/Employee/@name"
ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath1
Which (as expected) gets the first name attribute; just not sure how best to populate a Content Control drop-down from an XML document (see XML document below):
<?xml version="1.0"?>
<Employees>
<Employee name="Joe Blow">
<Email>[email protected]</Email>
<Extension>201</Extension>
</Employee>
<Employee name="Bob Smith">
<Email>[email protected]</Email>
<Extension>202</Extension>
</Employee>
</Employees>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下拉列表与所有其他内容控件不同 - 您需要为它们使用架构:演练:将内容控件绑定到自定义 XML 部件。
不过,您需要开始这样的代码:
这只是在下拉列表中设置 Joe Blow,但不会填充其余条目。上面的链接将告诉您如何做到这一点。
开始使用内容控件和 XML 映射时要考虑使用的另一个重要项目是 Word 内容控件工具包。它位于此处。
Drop down lists are different than all other Content Controls - you'll need to use a schema for them: Walkthrough: Binding Content Controls to Custom XML Parts.
You'll want to start with code like this though:
This just sets Joe Blow in the dropdown, but does not populate the rest of the entries. The link above will tell you how to do that.
Another great item to consider using when starting out with Content Controls and XML Mapping is the Word Content Control Toolkit. It's available here.