Word 2007 VBA:ActiveDocument.CustomXMLParts 和下拉列表

发布于 2024-09-07 15:39:40 字数 1175 浏览 3 评论 0原文

不确定将下拉列表内容控件正确绑定到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2024-09-14 15:39:40

下拉列表与所有其他内容控件不同 - 您需要为它们使用架构:演练:将内容控件绑定到自定义 XML 部件

不过,您需要开始这样的代码:

Sub BindtoDropDown()
    Dim ap As Document
    Set ap = ActiveDocument
    Dim cp As CustomXMLPart
    Set cp = ap.CustomXMLParts.Add
    cp.Load ("C:\Users\Me\Desktop\Employees.xml")
    Dim strXPath1 As String
    strXPath1 = "/Employees/Employee/@name"
    Dim ddCC As ContentControl
    Set ddCC = ap.ContentControls.Add(Type:=wdContentControlDropdownList)
    ddCC.XMLMapping.SetMapping (strXPath1)
End Sub

这只是在下拉列表中设置 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:

Sub BindtoDropDown()
    Dim ap As Document
    Set ap = ActiveDocument
    Dim cp As CustomXMLPart
    Set cp = ap.CustomXMLParts.Add
    cp.Load ("C:\Users\Me\Desktop\Employees.xml")
    Dim strXPath1 As String
    strXPath1 = "/Employees/Employee/@name"
    Dim ddCC As ContentControl
    Set ddCC = ap.ContentControls.Add(Type:=wdContentControlDropdownList)
    ddCC.XMLMapping.SetMapping (strXPath1)
End Sub

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文