none
adding data to Word defined object RRS feed

  • Question

  • Hi All,

    I'm wondering if anyone has figured out an effective way of associating an additional data value to a Microsoft.Office.Interop.Word defined object?

    From my research, I've seen that "Extension Property" is on the desired future feature list.  I've tried using a Dictionary with the Microsoft.Office.Interop.Word defined Object as an indexer.  I've tried saving and performing "Object.Equals" tests.  I've tried using the "ObjectIdGenerator" class.  My problem is two fold, I believe. 

    For Example, if I'm trying to associate a data value to the Revision object of Word. 

    1.  I access Revision Objects via their collection, thus every time I need to access the object, I receive a new instance of the object from the collection.  Thus no persistent object value.
    Microsoft.Office.Interop.Word _rev;
    _rev = Globals.ThisAddIn.Application.ActiveDocument.Sentences[indx].Revisions[jndx];

    2.  As changes to the document occur (additional revisions or revisions accepted/rejected, the parameters of the range which a revision is associated to changes.  Thus no persistent object parameter values.
    Microsoft.Office.Interop.Word _rev1, _rev2;
    _rev1 = Globals.ThisAddIn.Application.ActiveDocument.Sentences[indx].Revisions[jndx];
           // changes to the document occur.
    _rev2 = Globals.ThisAddIn.Application.ActiveDocument.Sentences[indx].Revisions[jndx];
    if( _rev1.Equals(_rev2))     // returns false

    I've successfully associated my data value with a revision by embedding it within the author name; setting upon revision creation and accessing during accept/reject operation, but the user experience of this would be really lame.

    So, to ask the question in a generic way, What approach would you suggest to associate an additional data item to an object that I can't define or extend, and to be able to associate and access that additional data item at non-contiguous times during Word AddIn operation. 

    Thanks for any assistance provided.

    billwa992

     

    Wednesday, June 1, 2011 5:14 PM

Answers

  • Hi Bill

    <<The one A-ha I got from your response is that range objects are automatically updated as the Document changes (not stated anywhere that I've seen when reading about Range Objects.  Thus, if I create a Range Object myself, then it's Start/End are kept updated as changes occur...>>

    Yes. This is a very important reason to use Start/End as little as possible. (Mostly, I only use them when defining a new Range in relation to an existing one; for example, from the current selection(.Range.Start) to the end of the document(.Content.End))

    For the most part, a Range object will remain reasonably stable in that it will "bracket" the same content, no matter what is added before or after. And if something is inserted into the content, that will become part of the Range. Things get "iffy" if something happens at the very start or the very end as that could damage the start- or end-marker. This particular (and, when you think about it, logical) quirk of Word's makes things rather difficult, even for the Word Team Programmers. A bit of trivia: when Track Changes is on, Delete and Backspace no longer work quite the same way: they won't let you delete "over" the border of a change. This problem with maintaining Range objects is behind that: you aren't allowed to destroy the start- /-end markers of the Revision Range.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by billwa992 Monday, June 13, 2011 6:52 PM
    Tuesday, June 7, 2011 1:43 PM
    Moderator

All replies

  • Hi Bill

    I think a Dictionary is as close as you're going to get to what you envision. The problem is certainly due things being so much "in flux" when a document is opened for editing in the Word application.

    The only other possibility that occurs to me is to set bookmarks around the relevant Ranges. These will usually move with the Range, which would make it possible to pick up the "object of interest" again with some reliability. Then you link that bookmark name with the data object (in the Dictionary, or whatever) and use the bookmark name to retrieve the object.

    If SmartTags or even XMLNodes were still supported, those might be better. But since they've been removed from Word in 2010...


    Cindy Meister, VSTO/Word MVP
    Thursday, June 2, 2011 7:13 AM
    Moderator
  • Thanks Cindy,

    I'll try your suggestion in a couple of days and get back to you on what/if I worked out.

    The one A-ha I got from your response is that range objects are automatically updated as the Document changes (not stated anywhere that I've seen when reading about Range Objects.  Thus, if I create a Range Object myself, then it's Start/End are kept updated as changes occur along with the Microsoft.Office.Interop.Word.Range objects are.

    More when I get to this later this week.  Thanks for the idea.

    Billwa992

    Monday, June 6, 2011 10:19 PM
  • Hi Bill

    <<The one A-ha I got from your response is that range objects are automatically updated as the Document changes (not stated anywhere that I've seen when reading about Range Objects.  Thus, if I create a Range Object myself, then it's Start/End are kept updated as changes occur...>>

    Yes. This is a very important reason to use Start/End as little as possible. (Mostly, I only use them when defining a new Range in relation to an existing one; for example, from the current selection(.Range.Start) to the end of the document(.Content.End))

    For the most part, a Range object will remain reasonably stable in that it will "bracket" the same content, no matter what is added before or after. And if something is inserted into the content, that will become part of the Range. Things get "iffy" if something happens at the very start or the very end as that could damage the start- or end-marker. This particular (and, when you think about it, logical) quirk of Word's makes things rather difficult, even for the Word Team Programmers. A bit of trivia: when Track Changes is on, Delete and Backspace no longer work quite the same way: they won't let you delete "over" the border of a change. This problem with maintaining Range objects is behind that: you aren't allowed to destroy the start- /-end markers of the Revision Range.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by billwa992 Monday, June 13, 2011 6:52 PM
    Tuesday, June 7, 2011 1:43 PM
    Moderator
  • Hi Bill

    Any update?


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 10, 2011 10:06 AM
    Moderator
  • Callvin/Cindy,

    Using a Dictionary with Range Objects as the Key with them matching the Range Objects of the Revisions seems to have provided a reliable approach to adding a data item to the Revision Object within my AddIn.

    Thanks, Billwa992

    • Marked as answer by billwa992 Monday, June 13, 2011 6:52 PM
    • Unmarked as answer by billwa992 Monday, June 13, 2011 6:52 PM
    Monday, June 13, 2011 6:52 PM