none
Selection Changed/Word busy and the Undo-Problem RRS feed

  • Question

  • Hi Group,

    we are developing a VSTO-AddIn for Word2010 - after a couple of weeks we are facing a problem, which we are not able to solve:

    Basically we have a TaskPane with a lot of entries, which are related to entries in the document. Similar to the Word-Sections-List (don't know the english word, I mean the list of topics, which you can show on the left side), which always emphasize the Topic, when you are scrolling down the document, we want to mark our sections in out list as well (keeping the cursor and our list in sync). 

    The only way we found is to catch "OnSelectionChange". This works.

    In our AddIn you are able to change those entries - changing means several changes in the WordDocument. After a couple of days we achieved, that those changes are cumulated in ONE UndoRecord - so the users does not see a lot of changes in the UndoHistory, but just one. And here comes the problem:

    When the user is doing an undo, word has to "undo" a couple of things. While undoing the SelectionChanged Event is thrown a couple of times. When the SelectionChanged Event is thrown our addin tries to track, where the Selection is, to emphasize the entry in the list - but when (e.g.) accessing the Range-object of the paragraphs-objects while word is in the Undo-Process we get an exception, that the document is not loaded correctly (or something like that) - or in other words, Word is idle we mustn't access any COM-objects. But we did not found any Word.IsIdle() calls. 

    Any ideas? suggestions?

    Thanks a lot and regards from Stuttgart

    Holger

    Friday, October 19, 2012 8:32 AM

Answers

  • Hallo Holger

    Stuttgart - we're almost neighbors :-)

    Unfortunately, I know of no way to discover whether Undo is causing an event to trigger, except for the content control events that have a InUndoRedo argument.

    Many years ago, VBA could capture it with a macro named EditUndo, but that no longer works. And it's not possible to re-purpose a split-button control in the Ribbon. That pretty much leaves us proverbially up the creek without the necessary tool.

    The only thing that occurs to me within the standard APIs is to use try...catch and, if it's this error, just let processing continue without doing anything.

    Other than that, you'd have to use the Window API to capture mouse clicks and the use of the arrows keys (the things that trigger SelectionChange) instead of relying on the event.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 19, 2012 2:20 PM
    Moderator