views:

28

answers:

1

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>
+1  A: 

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.

Otaku
I've read that example several times; there's no mention of drop-down content controls in there... ok they mention the word "drop-down" once in a sentence but there's no examples or instructions.
gravyface
My apologies, wrong link. I've corrected it above and also provided another link.
Otaku