none
Event Handler Interrupts Undo/Redo Records RRS feed

  • Question

  • I have an MS Word VBA project which defines an Event handler class for the purpose of overriding the DocumentBeforeClose event.  The class does not implement any other Event procedures, however it appears that merely instantiating the class causes Word to attempt invoking other Event procedures that are not defined.

    Specifically, my users have a workflow in which they select a block of text and type to overwrite it, then attempt to Redo that typing.  With my Event handler class instantiated, the first keypress seems to attempt to trigger a WindowSelectionChange event in my class (which no-ops) and that action interrupts the "typing" record on the undo/redo stack.  If the user continues to type a full word (e.g. "text", where the first "t" overwrote the previous selection) and then presses Redo, only the characters after the failed event will be added (e.g. "ext" in this example).

    Is there any way I could get around this?  I've investigated the UndoRecord object and CustomRecord concept, but can't think of any way to apply it here (and I need to support use of MS Word 2007 with my project anyway).

    Wednesday, September 24, 2014 6:32 PM

All replies

  • Hi Dan,

    Thanks for posint in MSDN forum.

    >> however it appears that merely instantiating the class causes Word to attempt invoking other Event procedures that are not defined.<<

    What event procedures did you mean?

    >> With my Event handler class instantiated, the first keypress seems to attempt to trigger a WindowSelectionChange event in my class (which no-ops) and that action interrupts the "typing" record on the undo/redo stack.  If the user continues to type a full word (e.g. "text", where the first "t" overwrote the previous selection) and then presses Redo, only the characters after the failed event will be added (e.g. "ext" in this example).<<

    As far as I know, the WindowSelectionChange always occurs when the selection changes in the active document window.

    Would you mind sharing with the code snippet to help us reproduce the issue?

    Best regards

    Fei


    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.

    Friday, September 26, 2014 1:42 AM
    Moderator
  • Hi Fei,

    I have a class module MagicEventHandler defined as follows:

    Public WithEvents objWord As Word.Application
    
    Private Sub objWord_DocumentBeforeClose(ByVal Doc As Document, ByRef bCancel As Boolean)
        bCancel = bNoDocClose
    End Sub
    

    And I instantiate it as follows in a separate code module:

    .....    
        Set objEventHandler = New MagicEventHandler
        RegisterEventHandler
    .....
    
    Sub RegisterEventHandler()
        Set objEventHandler.objWord = Word.Application
    End Sub
    

    If I comment out the instantiation, then the occurrence of the WindowSelectionChange event does not affect the records on the Undo stack.  I.e., without this code in place, typing to overwrite a selected block of text requires only a single Undo action.  With the code active, it requires two Undo actions (one for the typing after the event and a second for the typing before the event).

    -Dan

    Friday, September 26, 2014 2:13 PM
  • Hi Dan,

    Thanks for your detail information. I reproduced this issue successfully. Here are my steps:
    1. create a MagicEventHandler class module with code below:

    Public WithEvents objWord As Word.Application
    
    Private Sub objWord_DocumentBeforeClose(ByVal Doc As Document, ByRef bCancel As Boolean)
        bCancel = bNoDocClose
    End Sub
    

    2. copy the code below to ThisDocument module:

    Dim objEventHandler As MagicEventHandler
    Sub test()
        Set objEventHandler = New MagicEventHandler
        RegisterEventHandler
    End Sub
    
    
    
    Sub RegisterEventHandler()
        Set objEventHandler.objWord = Word.Application
    End Sub
    

    3. run the test sub

    4. type the "Text" in the document

    5. press Ctrl+Z to undo the record, the "Text" word in documet was removed correctly

    And this issue always occur after I run the test sub. And I can see the undo stack as fingure below:

    Since this issue is complex, I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.

    Sorry for any inconvenience and have a nice day!

    Best regards

    Fei 


    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.

    Tuesday, October 7, 2014 9:41 AM
    Moderator
  • Hi Dan,

    It would be helpful if you can provide me with environment details on which you reproduced the issue .

    Regards,

    Anush.

    Wednesday, October 22, 2014 2:57 PM
  • Hi Dan,

    I was not able to reproduce the issue on my Win 8.1 machine running Word (15.0.4659.1000). It would be helpful if you can provide me with your environment details.

    Based on my discussion with Fei Xue, the issue seems to be fixed with the latest updates. I would like to suggest you to update your Word if you have not updated.

    Regards,

    Anush.

    Thursday, October 30, 2014 4:49 PM