This may be more than you asked for, but since the VBA help for the majority of the CustomXMLPart properties and methods is a train wreck, I though I would post a working example to illustrate a few things the help fails to do:
HTML Code:
Option Explicit
Sub Demo()
Dim oCXParts As CustomXMLParts
Dim oCXNode As CustomXMLNode
Dim oCXChildNode As CustomXMLNode
Dim strData As String
Dim oCXNodes As CustomXMLNodes
'Demonstarates 1) The CustomXMLParts collection and adding CustomXMLParts 2) SelectByNamespace 3) SelectNodes 4) SelectSingleNode
'Provide the XML for the demonstration.
AddDemoParts
'Get the collection of custom xml parts with the given namespace. (There are 2 in this demo)
Set oCXParts = ActiveDocument.CustomXMLParts.SelectByNamespace("urn:invoice:namespace")
'Using the first XML part with matching namespace, get the first node matching the XPath expression (i.e., supplierID value = 1).
Set oCXNode = oCXParts(1).SelectSingleNode("//*[@supplierID = 1]")
'Do something meaningful:
strData = oCXNode.ParentNode.BaseName & ": "
For Each oCXChildNode In oCXNode.ParentNode.ChildNodes
If oCXChildNode.NodeType = msoCustomXMLNodeElement Then
strData = strData & oCXChildNode.BaseName & ": " & oCXChildNode.Text & " "
Else
strData = strData & oCXChildNode.Text & " "
End If
Next oCXChildNode
MsgBox Trim(strData)
'Using the second XML part with matching namespace, get the collection of all nodes matching the XPath expression (i.e., supplierID value = 1).
Set oCXNodes = oCXParts(2).SelectNodes("//*[@supplierID = 1]")
'Process each node in the collection of nodes. (There are three nodes).
For Each oCXNode In oCXNodes
strData = oCXNode.ParentNode.BaseName & ": "
For Each oCXChildNode In oCXNode.ParentNode.ChildNodes
If oCXChildNode.NodeType = msoCustomXMLNodeElement Then
strData = strData & oCXChildNode.BaseName & ": " & oCXChildNode.Text & " "
Else
strData = strData & oCXChildNode.Text & " "
End If
Next oCXChildNode
MsgBox Trim(strData)
Next oCXNode
lbl_Exit:
Exit Sub
End Sub
Sub AddDemoParts()
'Kill any existing XMLPart used previously for demo.
KillPilingUpParts
'Create two different demo XML Parts with the same namespace.
ActiveDocument.CustomXMLParts.Add "<?xml version='1.0' ?><Root xmlns='urn:invoice:namespace'>" _
& "<Item>Hammer<Supplier supplierID='1'><Name>Acme Tools</Name></Supplier></Item>" _
& "<Item>Cog<Supplier supplierID='2'><Name>Coswell Gogs</Name></Supplier></Item>" _
& "<Item>Sprocket<Supplier supplierID='3'><Name>Spacely Sprockets</Name></Supplier></Item>" _
& "<Item>Wrench<Supplier supplierID='1'><Name>Acme Tools</Name></Supplier></Item>" _
& "</Root>"
ActiveDocument.CustomXMLParts.Add "<?xml version='1.0' ?><Root xmlns='urn:invoice:namespace'>" _
& "<Item>Toilet Paper<Supplier supplierID='1'><Name>Proctor & Gamble</Name></Supplier></Item>" _
& "<Item>Wax<Supplier supplierID='2'><Name>Johnson & Johnson</Name></Supplier></Item>" _
& "<Item>Toilet Paper<Supplier supplierID='3'><Name>Kimberly Clarke</Name></Supplier></Item>" _
& "<Item>Soap<Supplier supplierID='1'><Name>Proctor & Gamble</Name></Supplier></Item>" _
& "<Item>Shaving Cream<Supplier supplierID='1'><Name>Proctor & Gamble</Name></Supplier></Item>" _
& "</Root>"
lbl_Exit:
Exit Sub
End Sub
Sub KillPilingUpParts()
Dim lngIndex As Long
For lngIndex = ActiveDocument.CustomXMLParts.Count To 4 Step -1
ActiveDocument.CustomXMLParts(lngIndex).Delete
Next lngIndex
lbl_Exit:
Exit Sub
End Sub