Class XMLNode (Word VBA)

The class XMLNode represents a single XML element applied to a document.

The main procedures of class XMLNode are Copy and Delete

Set

To use a XMLNode class variable it first needs to be instantiated, for example

Dim xml as XMLNode
Set xml = Dim strXPath As String: strXPath = 
ActiveDocument.SelectSingleNode(XPath:=strXPath)

The following procedures can be used to set variables of type XMLNode: Document.SelectSingleNode, FirstChild, LastChild, SelectSingleNode, NextSibling, ParentNode, PreviousSibling, XMLNodes.Item, Document.SelectNodes, SelectNodes, Attributes and ChildNodes

For Each

Here is an example of processing the XMLNode items in a collection.

Dim nod As XMLNode 
Dim strValid As String 
For Each nod In ActiveDocument.XMLNodes 
 nod.Validate 
 If nod.ValidationStatus <> wdXMLValidationStatusOK Then 
 strValid = strValid & nod.BaseName & vbTab & nod.ValidationErrorText & vbCrLf 
 End If 
Next 
MsgBox "The following elements do not validate against " &  "the schema." & vbCrLf & vbCrLf & strValid & vbCrLf &  "You should fix these elements before continuing."

Methods

These are the main methods of the XMLNode class

Copy - Copies the specified XML element, excluding XML markup, to the Clipboard.

ActiveDocument.SelectSingleNode.Copy

Delete - Deletes the specified XML element from an XML document.

ActiveDocument.SelectSingleNode.Delete

Other Methods

SelectNodes - Returns an XMLNodes collection that represents all the child elements that match the XPath parameter, in the order in which they appear within the specified XML element.

Dim strXPath As String: strXPath =  
Dim xmlsSelectNodes As XMLNodes
Set xmlsSelectNodes = ActiveDocument.SelectSingleNode.SelectNodes(XPath:=strXPath)

SelectSingleNode - Returns an XMLNode object that represents the first child element that matches the XPath parameter within the specified XML element.

Dim strXPath As String: strXPath =  
Dim xmlSelectSingleNode As XMLNode
Set xmlSelectSingleNode = ActiveDocument.SelectSingleNode.SelectSingleNode(XPath:=strXPath)

SetValidationError - Changes the validation error text displayed to a user for a specified node and forces Word to report a node as invalid.

ActiveDocument.SelectSingleNode.SetValidationError Status:=wdXMLValidationStatusCustom

Properties

Attributes returns an XMLNodes collection that represents the attributes for the specified element.

Dim xmlsAttributes As XMLNodes
Set xmlsAttributes = ActiveDocument.SelectSingleNode.Attributes

BaseName returns a String that represents the name of the element without any prefix.

Dim strBaseName As String
strBaseName = ActiveDocument.SelectSingleNode.BaseName

ChildNodes returns an XMLNodes collection that represents the child elements of a specified element.

Dim xmlsChildNodes As XMLNodes
Set xmlsChildNodes = ActiveDocument.SelectSingleNode.ChildNodes

FirstChild returns a DiagramNode object that represents the first child node of a parent node.

Dim xmlFirstChild As XMLNode
Set xmlFirstChild = ActiveDocument.SelectSingleNode.FirstChild

HasChildNodes returns a Boolean that represents whether an XML node has child nodes.

Dim booHasChildNodes As Boolean
booHasChildNodes = ActiveDocument.SelectSingleNode.HasChildNodes

LastChild returns an XMLNode object that represents the last child node of an XML element.

Dim xmlLastChild As XMLNode
Set xmlLastChild = ActiveDocument.SelectSingleNode.LastChild

Level returns a WdXMLNodeLevel constant that represents whether an XML element is part of a paragraph, is a paragraph, or is contained within a table cell or contains a table row.

Dim wxmLevel As WdXMLNodeLevel
wxmLevel = ActiveDocument.SelectSingleNode.Level

NamespaceURI returns a String that represents the Uniform Resource Identifier (URI) of the schema namespace for the specified object.

Dim strNamespaceURI As String
strNamespaceURI = ActiveDocument.SelectSingleNode.NamespaceURI

NextSibling returns an XMLNode object that represents the next element in the document that is at the same level as the specified element.

Dim xmlNextSibling As XMLNode
Set xmlNextSibling = ActiveDocument.SelectSingleNode.NextSibling

NodeType returns a WdXMLNodeType constant that represents the type of node.

Dim wxmNodeType As WdXMLNodeType
wxmNodeType = ActiveDocument.SelectSingleNode.NodeType

NodeValue returns or sets a String that represents the value of an XML element.

ActiveDocument.SelectSingleNode.NodeValue =

OwnerDocument returns a Document object that represents the parent document of the specified XML element.

Dim docOwnerDocument As Document
Set docOwnerDocument = ActiveDocument.SelectSingleNode.OwnerDocument

Parent returns an object that represents the parent object of the specified XMLNode object.

Dim objParent As Object
Set objParent = ActiveDocument.SelectSingleNode.Parent

ParentNode returns an XMLNode object that represents the parent element of the specified element.

Dim xmlParentNode As XMLNode
Set xmlParentNode = ActiveDocument.SelectSingleNode.ParentNode

PlaceholderText sets or returns a String that represents the text displayed for an element that contains no text.

ActiveDocument.SelectSingleNode.PlaceholderText =

PreviousSibling returns an XMLNode object that represents the previous element in the document that is at the same level as the specified element.

Dim xmlPreviousSibling As XMLNode
Set xmlPreviousSibling = ActiveDocument.SelectSingleNode.PreviousSibling

Range returns a Range object that represents the portion of a document that is contained in the specified object.

Dim rngRange As Range
Set rngRange = ActiveDocument.SelectSingleNode.Range

Text returns or sets the text contained within the XML element.

ActiveDocument.SelectSingleNode.Text =

ValidationErrorText returns a String that represents the description for a validation error on an XMLNode object.

Dim strValidationErrorText As String
strValidationErrorText = ActiveDocument.SelectSingleNode.ValidationErrorText

ValidationStatus returns a WdXMLValidationStatus constant that represents whether an element or attribute is valid according to the attached schema.

Dim wxmsValidationStatus As WdXMLValidationStatus
wxmsValidationStatus = ActiveDocument.SelectSingleNode.ValidationStatus

WordOpenXML returns a String that represents the XML for the node in the Microsoft Word Open XML format.

Dim strWordOpenXML As String
strWordOpenXML = ActiveDocument.SelectSingleNode.WordOpenXML

XML

Dim strXML As String
strXML = ActiveDocument.SelectSingleNode.XML

XMLNodes.Count returns a Long that represents the number of XML elements in the collection.

Dim lngCount As Long
lngCount = ActiveDocument.SelectSingleNode.Attributes.Count

XMLNodes.Parent returns an object that represents the parent object of the specified XMLNodes object.

Dim objParent As Object
Set objParent = ActiveDocument.SelectSingleNode.Attributes.Parent