Unsure of the best way to bind a开发者_StackOverflow 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.
精彩评论