Word 2007 VBA:ActiveDocument.CustomXMLParts和下拉列表 [英] Word 2007 VBA: ActiveDocument.CustomXMLParts and Drop-down lists

查看:106
本文介绍了Word 2007 VBA:ActiveDocument.CustomXMLParts和下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定将下拉列表内容控件正确绑定到XML文件的最佳方法:我所获得的只是第一项.

我假设我必须遍历XML文档,计算项目数,然后相应地在控件上调用.Add方法,但是我不确定如何在VBA中做到这一点. /p>

这就是我所拥有的:

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>jblow@example.com</Email>
     <Extension>201</Extension>
   </Employee>
   <Employee name="Bob Smith">
     <Email>bsmith@example.com</Email>
     <Extension>202</Extension>
   </Employee>
</Employees>

解决方案

下拉列表与所有其他内容控件均不同-您需要为其使用以下架构:

这仅在下拉菜单中设置Joe Blow,但不会填充其余条目.上面的链接将告诉您如何执行此操作.

Word内容控制工具包是在开始使用内容控件和XML映射时要考虑的另一个重要项目.可以在此处使用.

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>jblow@example.com</Email>
     <Extension>201</Extension>
   </Employee>
   <Employee name="Bob Smith">
     <Email>bsmith@example.com</Email>
     <Extension>202</Extension>
   </Employee>
</Employees>

解决方案

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.

这篇关于Word 2007 VBA:ActiveDocument.CustomXMLParts和下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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