none
Output Word2010 text to a XML file RRS feed

  • Question

  • Hi,

    I am thinking about special Word2010 XML output using VBA or VSTO addin. The source Word document is not made commonly, it is made using VBA macro or VSTO addin. So there are following feature and constraint.

    1. The Word document is mostly composed of ADDIN field. The ADDIN field's data member holds the XML element name and attribute. So ADDIN field itself expresses a XML element. I want output ADDIN field's result surrounding the XML element like this.

    <element-name attribute="value">The result of ADDIN field</element-name>

    element-name: It is contained in the ADDIN field's Data member in some format.
    attribute, value: It is contained in the ADDIN field's Data member in some format.
    The result of ADDIN field: ADDIN field seems to not have result member. However Word displays the inner text of ADDIN field as field result.

    2. The Word document contains the shape object that is converted to inline shape by the ConvertToInlineShape method. I want to skip this shape. If I enumerate the ActiveDocument.Content.Characters collection, it is appeared as "/" character.

    Is there any excellent technique to do this using VBA or VSTO? Please give me an advice!

    Thanks,


    Thursday, June 23, 2011 3:16 AM

Answers

  • 1. Are you sure that the ADDIN does not have a Result member? It seems to here, even when it appears to have no content.

    I know that it is not usually possible to /set/ the result of an ADDIN - you first have to change the field type to something else (e.g. QUOTE), then set the Result, then change the field type back to ADDIN. It is possible that you might be able to do that to get the result.

    2. If you cannot do that, another approach might be to select the ADDIN field then using Selection.Range.XML or Selection.Range.WordOpenXML to get the XML and process it. e.g. if you get the .XML, a simple result such as "abc def" would appear like this:

    <w:fldChar w:fldCharType="separate"/></w:r><w:r><w:t>abc def</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/> 

    So you might need to transform the WordML to get the result you want.

    3. Not sure, but perhaps that technique in (2) will work better for your shapes, and may be a better way to approach your project. 


    Peter Jamieson
    • Marked as answer by tmakita Thursday, June 23, 2011 12:57 PM
    Thursday, June 23, 2011 9:20 AM

All replies

  • 1. Are you sure that the ADDIN does not have a Result member? It seems to here, even when it appears to have no content.

    I know that it is not usually possible to /set/ the result of an ADDIN - you first have to change the field type to something else (e.g. QUOTE), then set the Result, then change the field type back to ADDIN. It is possible that you might be able to do that to get the result.

    2. If you cannot do that, another approach might be to select the ADDIN field then using Selection.Range.XML or Selection.Range.WordOpenXML to get the XML and process it. e.g. if you get the .XML, a simple result such as "abc def" would appear like this:

    <w:fldChar w:fldCharType="separate"/></w:r><w:r><w:t>abc def</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/> 

    So you might need to transform the WordML to get the result you want.

    3. Not sure, but perhaps that technique in (2) will work better for your shapes, and may be a better way to approach your project. 


    Peter Jamieson
    • Marked as answer by tmakita Thursday, June 23, 2011 12:57 PM
    Thursday, June 23, 2011 9:20 AM
  • Hi Peter,

    Thank you for your advice.

    1. You are right. Before reading your post I made a debug VBA code that outputs the Result of ADDIN field. It actually has the result. This document was made by my VBA code using technique you suggested. (First make "QUOTE" field and set temporary result. Then change the field code to "ADDIN". The user can edit text from the Word GUI. But the edited result text belongs to the "ADDIN" field's result.)

    2. I think that it is mostly impossible to enumerate this document from start to end considering field and field result to make XML element. This may be Word object model limitation. Your suggestion is helpful. I thought mostly the same thing. My idea is to use ActiveDocument.WordOpenXML property. If I can pass this string to the XmlTextReader class, I can enumerate the whole document freely.

    Thanks,

    Thursday, June 23, 2011 12:57 PM