Binding Content control to xml list

M

Mike

Hi,
i have an xml that contains elements that looks like this:

<Authors>
<Author>
<Id>1
<Name>Joe Smith</Name>
</Author>
<Author>
<Id>2
<Name>Tom Smith</Name>
</Author>
<Author>
<Id>3
<Name>Mark Smith</Name>
</Author>
</Author>

I would like to bind this to two content controls that display Id and Name.
The problem is, how do i handle XML nodes that may contain 1 to N amount of
elements? In .NET we could use something like a Repeater control. Is it
possible to do something similar in word where it would display all child
Author nodes?

thanks
 
G

Greg Maxey

Mike,

I know a little about mapping CCs and even less about XML in general.
However, tinkering with your XML a little, I was able to scratch together
the following. I used a 3 X 2 table to hold the six CCs:

Sub AddContentControlsAndMapToLocalXML()
Dim oCC As Word.ContentControl
Dim oCustomPart As Office.CustomXMLPart
Dim xmlPart As String
Dim i As Long
Dim xPath As String
ClearXMLParts
xmlPart = "<?xml version='1.0'
encoding='utf-8'?><Authors><Author><Id>1</Id>" _
& "<Name>Joe Smith</Name></Author><Author><Id>2</Id><Name>Tom
Smith</Name>" _
& "</Author><Author><Id>3</Id><Name>Mark
Smith</Name></Author></Authors>"
Set oCustomPart = ActiveDocument.CustomXMLParts.Add(xmlPart)
For i = 1 To oCustomPart.DocumentElement.ChildNodes.Count
Set oCC = ActiveDocument.ContentControls.Add(wdContentControlText,
Selection.Tables(1).Cell(i, 2).Range)
xPath = "/Authors/Author[" & i & "]/Name[1]"
oCC.XMLMapping.SetMapping xPath
Next i
For i = 1 To oCustomPart.DocumentElement.ChildNodes.Count
Set oCC = ActiveDocument.ContentControls.Add(wdContentControlText,
Selection.Tables(1).Cell(i, 1).Range)
xPath = "/Authors/Author[" & i & "]/Id[1]"
oCC.XMLMapping.SetMapping xPath
Next i
End Sub


'Run ClearXMLParts prior to testing these examples.
Sub ClearXMLParts()
Dim i As Long
'MsgBox ActiveDocument.CustomXMLParts.Count
For i = ActiveDocument.CustomXMLParts.Count To 4 Step -1
ActiveDocument.CustomXMLParts(i).Delete
Next i
End Sub

Probably rough as a hand hewn plank, but the best I can offer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top