Get to know .InsertXML

Published 06 November 03 04:50 PM | chris 
As I began to work with the XML object model in Word 2003 I found that it was fairly extensive. However, there are often times when I want to do something “XML” related that the XML object model did not support. As an example, if I want to insert Arbitrary XML into the document without attaching a schema, how do I do that? .InsertXML to the rescue!!!!

.InsertXML is a method of the Range Object. The online help describes this feature:

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.

So in a nutshell you can quickly insert XML into a Word Range Object. An example might be:

Selection.InsertXML "<CustomerName>Chris Kunicki</CustomerName>”

The Word document will now contain a node named CustomerName and the text in the node is "Chris Kunicki”.

A few quick notes about the help description posted above. First, the Word XML Content Development Kit is not online yet, though I’m sure its on its way. Second, it mentions “If the specified XML text cannot be inserted into the specified range or selection, an error message is displayed.” and the help is not kidding. No matter what is wrong, you will basically get a generic error saying you cannot insert the XML. So if you use .InsertXML, Word assumes you are taking responsibility for the XML you are inserting and if it’s incorrect some how that is your problem.

Lets consider another example of inserting Arbitrary XML into a word document

Private Sub InsertRawXML()
    Dim sXML As String
    sXML = "<Customer xmlns='testNameSpace'>" & _
            "<Name>Fred</Name><Address state='CA'>Palm Springs</Address>" & _
           "</Customer>"
    Selection.InsertXML (sXML)
End Sub

This is what appears in Word:
WordML.gif
Another thing you can with .InsertXML is insert WordML. WordML is the schema based XML syntax used by Word for formatting. The following code snippet:

Private Sub InsertWordML()
    Dim sXML As String
    sXML = "<wordDocument xmlns='http://schemas.microsoft.com/office/word/2003/wordml'><body>" & _
              "<p><r><t xml:space='preserve'>Example of WordML and InsertXML run at </t></r>" & _
              "<r><rPr><b/></rPr><t>" & Now() & "</t></r></p><p/><p/></body></wordDocument>"
    'Insert WordML
    Selection.InsertXML (sXML)
End Sub

Produces the following format in Word:
WordML.gif
So to wrap this up, .InsertXML is a brute force technique to stuff Arbitrary XML and WordML into a Word document. Once you know its there, you’ll use it a million times.

Comments

# InsertXml - not getting the same result as opening xml file | keyongtech said on January 21, 2009 5:34 PM:

PingBack from http://www.keyongtech.com/1497408-insertxml-not-getting-the-same

Anonymous comments are disabled