InsertXML Method

Inserts the specified XML text into the specified range or selection. If the specified range or selection contains text, the InsertXML method replaces the existing text.

expression.InsertXML(XML, Transform)

expression Required. An expression that returns one of the objects in the Applies To list.

XML    Required String. The XML text to insert.

Transform    Optional Variant. The transform to apply to the inserted XML text.


Use the InsertXML method to insert text marked up with either arbitrary XML or Word XML. The XML must be well formed. If it uses Word XML, then it must also be valid according to the Word XML schema. For more information on the Word XML schema, please refer to the Word XML Content Development Kit, which you can find on the Microsoft Developer Network (MSDN) Web site.

If the specified XML text cannot be inserted into the specified range or selection, an error message is displayed.


The following example adds the specified schema to the Schema Library, attaches the newly added schema to the active document, and then inserts an XML element with accompanying text.

ActiveDocument.Range.InsertXML _
        XML:="<example>This is an example.</example>"

If the elementFormDefault setting in the schema is set to "qualified", the XML text specified in the XML parameter must contain the necessary namespace reference. In this case, the XML text passed in for the XML parameter in the preceding example would need to be the following, where "namespace" is the target namespace of the schema.

ActiveDocument.Range.InsertXML _
        "<example namespace"">This is an example.</example>"

You can specify the target namespace whether the elementFormDefault setting is specified in the schema or not. Specifying the namespace can alleviate any possible errors in code where the target namespace is required.

Applies to | Range Object | Selection Object