Referencing XML nodes in Word 2003 VBA

P

ppsza

I am trying to access xml elements in a word 2003 documen
(ActiveDocument) in VBA. I have discovered the XMLNodes collection o
the ActiveDocument, but was disappointed to discover that elements o
the collection can be referrenced by ordinal index number only! I a
wondering if anyone might know of a way to directly access an elemen
by its name, instead.

Here is the code that allows me to do it with ordinal index numbers:

ActiveDocument.XMLNodes(1).ChildNodes(1).Text

I've tried this, which doesn't work:

ActiveDocument.XMLNodes(1).ChildNodes("MyNode").Text

Thanks
 
D

Doug Robbins - Word MVP

I believe that you will need to iterate through the collection, checking the
..BaseName property of each node until you get a match. The following
example is from the VBE help file:

Sub AddIDAttribute()
Dim objElement As XMLNode
Dim objAttribute As XMLNode

For Each objElement In ActiveDocument.XMLNodes
If objElement.NodeType = wdXMLNodeElement Then
If objElement.BaseName = "book" Then

Set objAttribute = objElement.Attributes _
.Add("author", objElement.NamespaceURI)

objAttribute.NodeValue = "David Barber"

Exit For
End If
End If
Next
End Sub
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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