CustomXMLNode.InsertNodeBefore method (Office)

Inserts a new node just before the context node in the tree.


expression.InsertNodeBefore(Name, NamespaceURI, NodeType, NodeValue, NextSibling)

expression An expression that returns a CustomXMLNode object.


Name Required/Optional Data type Description
Name Optional String Represents the base name of the node to be added.
NamespaceURI Optional String Represents the namespace of the element to be added. This parameter is required if adding nodes of type msoCustomXMLNodeElement or msoCustomXMLNodeAttribute; otherwise, it is ignored.
NodeType Optional MsoCustomXMLNodeType Specifies the type of the node to be added. If the parameter is not specified, it is assumed to be a node of type msoCustomXMLNodeElement.
NodeValue Optional String Used to set the value of the node to be added for those nodes that allow text. If the node doesn't allow text, the parameter is ignored.
NextSibling Optional CustomXMLNode Represents the context node.


If the context node is not present when adding a node of type msoCustomXMLNodeElement, msoCustomXMLNodeComment, or msoCustomXMLNodeProcessingInstruction, the new node is added to the last child of the context node. If the operation would result in an invalid tree structure, the insertion is not performed and an error message is displayed.


The following example adds a custom part and then finds a node in that part by using an XPath expression. The code then inserts a node before the found node.

Dim cxp1 As CustomXMLPart 
Dim cxn As CustomXMLNode 
With ActiveDocument 
   ' Add a custom xml part. 
   .CustomXMLParts.Add "<invoice>" 
   ' Returns the first custom xml part with the given root namespace. 
   Set cxp1 = .CustomXMLParts("urn:invoice:namespace")              
   ' Get node using XPath.                              
   Set cxn = cxp1.SelectSingleNode("//*[@supplier = "Contoso"]")  
   ' Insert a node before the single node selected previously. 
    cxn.InsertNodeBefore("discount", "urn:invoice:namespace")   
 End With

See also

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.