none
Using InsertXML with Word 2010 to insert custom XML (i.e. pink tagged XML) RRS feed

  • Question

  • Hello,

     

    I am trying to insert custom XML tags (i.e. pink XML tags from Word 2003) into a Word 2010 document.  I am able to insert them just fine if I manually click on the XML structure pane on the right and click on the XML element.  However, I need to do this problematically using the "insertXML" method.

     

     

    So I tried using the InsertXML to insert the following wordML syntax:

     

     

    <w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xml:space="preserve">
    <w:body>
    	<w:p>
    	<LISRef number= "12" congress= "109" title= "khalid test" url= "http://www.cnn.com/" type= "houseBill" >
    	<w:r>
    	<w:t>
    	</w:t>
    	</w:r>
    	<w:hlink w:dest="http://www.cnn.com/">
    	<w:r>
    	<w:rPr>
    	<w:rStyle w:val="HyperLink"/>
    	</w:rPr>
    	<w:t>test2
    	</w:t>
    	</w:r>
    	</w:hlink>
    	<w:r>
    	<w:t>
    	</w:t>
    	</w:r>
    	</LISRef>
    	</w:p>
    </w:body>
    </w:wordDocument>
    
    


     

     

    It inserts just fine - but the XML tags are not present!  Only the text portion is.

     

    So then, I tried using the OpenXML format and wrapping it in flat package;  and then I converted the custom XML sytanx as well.  The full string passed to InsertXML(...) looked as such:

     

    <pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"> 
    <pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml"> 
    <pkg:xmlData> 
    <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> 
    <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/> 
    </Relationships> 
    </pkg:xmlData> 
    </pkg:part> 
    <pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"> 
    <pkg:xmlData> 
    <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> 
    <w:body> 
    	<w:p w:rsidR="00126E28" w:rsidRDefault="00FB5DD2"> 
    	<w:pPr> 
    	<w:pStyle w:val="BodyText" /> 
    	</w:pPr> 
    	<w:customXml w:uri="crs_report" w:element="LISRef"> 
    	<w:customXmlPr> 
    	<w:attr w:name="congress" w:val="109" /> 
    	<w:attr w:name="number" w:val="12" /> 
    	<w:attr w:name="title" w:val="khalid test" /> 
    	<w:attr w:name="type" w:val="houseBill" /> 
    	<w:attr w:name="url" w:val="http://www.cnn.com" /> 
    	</w:customXmlPr> 
    	<w:r w:rsidR="00126E28"> 
    	<w:t>test2
    	</w:t> 
    	</w:r> 
    	</w:customXml> 
    	</w:p> 
    </w:body> 
    </w:document> 
    </pkg:xmlData> 
    </pkg:part> 
    </pkg:package>
    
    


     

     

     

    Again, no runtime errors, but it doesn't actually insert the XML tag -it again only inserts the text portion.

     

    What am I doing wrong here?  

     

    Thanks in advance - I've been spending way too much time on this problem.

    On a side note:  We are still u sing the old custom XML (pink XML tags), because we have purchased an add-on (from a company called "i4i" which won the lawsuit against Microsoft), which re-inserts the XML tags once the Word 2010 loads up.

     


    Khalid K.


    Khalid K.
    Tuesday, August 16, 2011 8:50 PM

Answers

  • Hi Khalid

    <<Microsoft's documentation on this particular functionality is severely lacking>>

    That's understandable, since the funcitonality is deprecated. That means not-supported. Microsoft openly says it should not be used and won't discuss workarounds to "force" Word to use it, even if there are loopholes. If you have a problem with that, the people to complain to are those at i4i, not Microsoft...

    <<So I figured since the above works absolutely fine in Word 2010, that the insertXML(...) method should also be able to accomplish the same thing.  But I guess I was wrong in this assumption?>>

    Any time you insert file content (RTF, HTML, Word's own XML), Word uses some kind of converter to integrate what's coming in with how Word has to function "under the covers". From what I've heard in discussions among fellow devs who know something about XML, in conjunction with that court decision, the sticking point with the patent was not the pink tags, themselves, but how Word handled managing the "custom XML" in memory. If my colleagues (not MS people, just to be clear!) who've studied that are correct, then conversion of WordML or WordOpenXML to pink tags would definitely fall under the category of patent infringement. Just inserting the pink tags might not. But that's just a guess on my part and should in no way be interpreted as knowledge about what's actually taking place. I simply do not know and Microsoft doesn't discuss any details about this kind of thing.

    Basically, if something is deprecated or undocumented then you use it at your own risk and there's no guarantee it will work, under any circumstances. And there's no support if it stops working.


    Cindy Meister, VSTO/Word MVP
    Thursday, August 18, 2011 8:50 AM
    Moderator

All replies

  • Hi Khalid

    The functionality has been deprecated and more or less removed in Word 2010. Because of that lawsuit. So I guess i4i had better offer a solution to create the tags in the first place, "programmatically". Or you'll need to discuss with them how to generate an Open XML document such that their tool can recognize the tags. Because Word removes them when the document is opened.


    Cindy Meister, VSTO/Word MVP
    Wednesday, August 17, 2011 8:29 AM
    Moderator
  • Hi Cindy,

     

    So what I find weird Cindy is that there is a workaround to insert the custom XML programatically using the following syntax where selectedRange is a "Range" object:

     

     

    selectedRange.XMLNodes.Add("LISRef", "", ref missing);
    

     

    This indeed does insert the XML pink tag into the Word 2010 document.  So I figured since the above works absolutely fine in Word 2010, that the insertXML(...) method should also be able to accomplish the same thing.  But I guess I was wrong in this assumption?

    Thanks again Cindy - Microsoft's documentation on this particular functionality is severely lacking, so I humbly appreciate your knowledgeable, expert responses.

     


    Khalid K.
    Wednesday, August 17, 2011 5:59 PM
  • Hi Khalid

    <<Microsoft's documentation on this particular functionality is severely lacking>>

    That's understandable, since the funcitonality is deprecated. That means not-supported. Microsoft openly says it should not be used and won't discuss workarounds to "force" Word to use it, even if there are loopholes. If you have a problem with that, the people to complain to are those at i4i, not Microsoft...

    <<So I figured since the above works absolutely fine in Word 2010, that the insertXML(...) method should also be able to accomplish the same thing.  But I guess I was wrong in this assumption?>>

    Any time you insert file content (RTF, HTML, Word's own XML), Word uses some kind of converter to integrate what's coming in with how Word has to function "under the covers". From what I've heard in discussions among fellow devs who know something about XML, in conjunction with that court decision, the sticking point with the patent was not the pink tags, themselves, but how Word handled managing the "custom XML" in memory. If my colleagues (not MS people, just to be clear!) who've studied that are correct, then conversion of WordML or WordOpenXML to pink tags would definitely fall under the category of patent infringement. Just inserting the pink tags might not. But that's just a guess on my part and should in no way be interpreted as knowledge about what's actually taking place. I simply do not know and Microsoft doesn't discuss any details about this kind of thing.

    Basically, if something is deprecated or undocumented then you use it at your own risk and there's no guarantee it will work, under any circumstances. And there's no support if it stops working.


    Cindy Meister, VSTO/Word MVP
    Thursday, August 18, 2011 8:50 AM
    Moderator