XMLAfterInsert Event
Collapse the table of content
Expand the table of content

Document.XMLAfterInsert Event (Word)

Office 2013 and later

Contribute to this content

Use GitHub to suggest and submit changes. See our guidelines for contributing to VBA documentation.

Occurs when a user adds a new XML element to a document. If more than one element is added to the document at the same time (for example, when cutting and pasting XML), the event fires for each element that is inserted.

Private Sub expression _XMLAfterInsert(NewXMLNode, InUndoRedo)

expression A variable that represents a Document object that has been declared by using the WithEvents keyword in a class module. For information about using events with a Document object, see Using Events with the Document Object.




Data Type





The newly added XML node.




True indicates the action was performed using the Undo or Redo feature in Microsoft Word.

If the InUndoRedo parameter is True, never change the XML in a document while the XMLAfterInsert and XMLBeforeDelete events are running.

If the InUndoRedo parameter is False, you can insert and delete the XML in the document, but be careful that the XMLAfterInsert and XMLBeforeDelete events will not try to cancel each other out, causing an infinite loop. You can prevent infinite loops by using a global Boolean variable and check for that at the beginning of the error handler, as shown in the following example.

Dim blnIsXMLInsertRunning As Boolean 
Private Sub Document_XMLAfterInsert(ByVal DeletedRange As Range, _ 
 ByVal OldXMLNode As XMLNode, ByVal InUndoRedo As Boolean) 
 If blnIsXMLInsertRunning = False Then 
 blnIsXMLInsertRunning = True 
 'Insert your event code here. 
 Exit Sub 
 End If 
End Sub

The following example validates a newly added node and if the node is not valid, displays a message describing the validation error.

Private Sub Document_XMLAfterInsert(ByVal NewXMLNode As XMLNode, _ 
 ByVal InUndoRedo As Boolean) 
 If NewXMLNode.ValidationStatus <> wdXMLValidationStatusOK Then 
 MsgBox NewXMLNode.ValidationErrorText 
 End If 
End Sub
© 2016 Microsoft