none
How to show colors for XML in Word RRS feed

  • Question

  • Hi all

    I would like to insert an XML string into a word document and would like to have a Visual Studio XML Editor like color style for it, just like the following,

    <Properties Key="Type">

       <Value>

          <string>Unknown</string>

        </Value>

     </Properties>

    But when I use code Range.Text = xmlString; I just got black text like this

    <URL>

      <AssetID>a9c03f89-577a-4d50-8214-a171e6304b9c</AssetID>

      <Device>Metadata</Device>

      <Domain>DEFAULTDOMAIN</Domain>

    </URL>

    Is there an API that I can use to easily format the color for the XML?

    Thank you!

    Friday, January 3, 2014 2:19 PM

Answers

  • Hi,

    According to your description, you want an API to easily format the color for the XML which seems as the effect of the Visual Studio XML Editor.

    However, I’m afraid there is no API to achieve the goal.

    Formatting the color for the XML is a complex task. In Word Object model, we could set a string as the text of a range and edit the font color, but we cannot set the color of different characters with several kind of colors, unless selecting different characters and editing the color with the code “Selection.Font.ColorIndex = wdBlue”. In addition, It’s difficult to find the key words of the XML and format the color.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, January 6, 2014 6:24 AM
    Moderator
  • Hi Bruce

    Expanding on Luna's reply:

    You would have to essentially duplicate what Internet Exporer or Visual Studio does to produce the effect you wish. In essence, these use a Transformation (XSLT) on the XML in order to display it with color and indents. Whereas Internet Explorer transforms the XML into HTML, for Word you'd need to transform the XML string into valid Word Open XML, in the OPC flat file format, then use the Range.InsertXML method in order to place it into a document with the formatting.

    As Luna says, this is NOT a trivial task and doesn't really fall into category of "Word API", which the topic this forum supports. You can find out more about the Word Open XML vocabulary at OpenXMLDeveloper.org; it's also documented in an ECMA standard that can be downloaded from the ECMA site.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Bruce Yong Wednesday, January 8, 2014 2:08 AM
    Monday, January 6, 2014 11:39 AM
    Moderator
  • Hi Bruce

    Your chance is better with HTML, but there's nothing in the Word API that will let you "stream" the HTML into a document with formatting. Since HTML is not Word's "native" file format a converter is required to "transform" the HTML into Word Open XML. Word does provide such a converter for opening files and pasting information from the Clipboard.

    So you need to either place the HTML on the Clipboard, then use the Paste command to insert it. Or save the HTML to a file and use the InsertFile method.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Bruce Yong Wednesday, January 8, 2014 2:09 AM
    Tuesday, January 7, 2014 9:24 AM
    Moderator

All replies

  • Hi,

    According to your description, you want an API to easily format the color for the XML which seems as the effect of the Visual Studio XML Editor.

    However, I’m afraid there is no API to achieve the goal.

    Formatting the color for the XML is a complex task. In Word Object model, we could set a string as the text of a range and edit the font color, but we cannot set the color of different characters with several kind of colors, unless selecting different characters and editing the color with the code “Selection.Font.ColorIndex = wdBlue”. In addition, It’s difficult to find the key words of the XML and format the color.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, January 6, 2014 6:24 AM
    Moderator
  • Hi Bruce

    Expanding on Luna's reply:

    You would have to essentially duplicate what Internet Exporer or Visual Studio does to produce the effect you wish. In essence, these use a Transformation (XSLT) on the XML in order to display it with color and indents. Whereas Internet Explorer transforms the XML into HTML, for Word you'd need to transform the XML string into valid Word Open XML, in the OPC flat file format, then use the Range.InsertXML method in order to place it into a document with the formatting.

    As Luna says, this is NOT a trivial task and doesn't really fall into category of "Word API", which the topic this forum supports. You can find out more about the Word Open XML vocabulary at OpenXMLDeveloper.org; it's also documented in an ECMA standard that can be downloaded from the ECMA site.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Bruce Yong Wednesday, January 8, 2014 2:08 AM
    Monday, January 6, 2014 11:39 AM
    Moderator
  • Thanks Luna and Cindy. I seems that unfortunately I have to implement it by myself.

    In addition to XML, I have an HTML string that includes tables, and I would like to insert it to Word and have it displayed with the same effect as Internet Explorer has, that is, not to have the HTML tags displayed. Is there a Word API to do so?

    Thank you!

    Tuesday, January 7, 2014 2:01 AM
  • Hi Bruce

    Your chance is better with HTML, but there's nothing in the Word API that will let you "stream" the HTML into a document with formatting. Since HTML is not Word's "native" file format a converter is required to "transform" the HTML into Word Open XML. Word does provide such a converter for opening files and pasting information from the Clipboard.

    So you need to either place the HTML on the Clipboard, then use the Paste command to insert it. Or save the HTML to a file and use the InsertFile method.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Bruce Yong Wednesday, January 8, 2014 2:09 AM
    Tuesday, January 7, 2014 9:24 AM
    Moderator