Macro onAfter events not firing

  • 1
  • Problem
  • Updated 3 weeks ago
I came across a weird bug, the onAfter events are not captured in a macro if it's run from the topic context menu. See the example code below. All four event functions fire if I run the macro from the macro editor, but only the two onBefore event functions fire if I run the macro from the topic context menu. Can anyone suggest a solution or a workaround? The idea of the macro I'm writing is that it remembers some of the topic values and then tracks their changes and records them to a table in another topic.

'#Language "WWB-COM"


Option Explicit

Dim WithEvents m_EventBeforeObjectModified As Event ' An event that is sent out from the API
Dim WithEvents m_EventAfterObjectModified As Event ' An event that is sent out from the API
Dim WithEvents m_EventBeforeSelectionChanged As Event ' An event that is sent out from the API
Dim WithEvents m_EventAfterSelectionChanged As Event ' An event that is sent out from the API



Sub Main

            Set m_EventBeforeObjectModified = Events.AddDocumentEvent(MmDocumentEventFlags.mmEventFlagObjectModified, True, False, Nothing) ' this one if OK
            Set m_EventAfterObjectModified = Events.AddDocumentEvent(MmDocumentEventFlags.mmEventFlagObjectModified, False, True, Nothing) ' will not fire if the macro is run from the topic context menu
            Set m_EventBeforeSelectionChanged = Events.AddDocumentEvent(MmDocumentEventFlags.mmEventFlagObjectModified, True, False, Nothing) ' will not fire if the macro is run from the topic context menu
            Set m_EventAfterSelectionChanged = Events.AddDocumentEvent(MmDocumentEventFlags.mmEventFlagObjectModified, False, True, Nothing) ' will not fire if the macro is run from the topic context menu


            Wait 60 ' run this macro for 60 seconds


            Set m_EventBeforeObjectModified = Nothing
            Set m_EventAfterObjectModified = Nothing
            Set m_EventBeforeSelectionChanged = Nothing
            Set m_EventAfterSelectionChanged = Nothing

            MsgBox("complete")

End Sub


Sub m_EventBeforeObjectModified_Fire(ByVal i_EventFlag As Long, ByVal e_Time As MmEventTime, ByVal m_Object As Object, ByRef p_Extra As Variant)
    Debug.Print("m_EventBeforeObjectModified_Fire")
End Sub


Sub m_EventAfterObjectModified_Fire(ByVal i_EventFlag As Long, ByVal e_Time As MmEventTime, ByVal m_Object As Object, ByRef p_Extra As Variant)
    Debug.Print("m_EventAfterObjectModified_Fire")
End Sub


Sub m_EventBeforeSelectionChanged_Fire(ByVal i_EventFlag As Long, ByVal e_Time As MmEventTime, ByVal m_Object As Object, ByRef p_Extra As Variant)
    Debug.Print("m_EventBeforeSelectionChanged_Fire")
End Sub


Sub m_EventAfterSelectionChanged_Fire(ByVal i_EventFlag As Long, ByVal e_Time As MmEventTime, ByVal m_Object As Object, ByRef p_Extra As Variant)
    Debug.Print("m_EventAfterSelectionChanged_Fire") 
End Sub
Photo of Michael

Michael

  • 3 Posts
  • 0 Reply Likes
  • Desperate

Posted 3 weeks ago

  • 1
Photo of Francis Procaccia

Francis Procaccia, Employee

  • 7 Posts
  • 6 Reply Likes
OnAfter events won't fire until the current UI command completes, which in your case takes 60 seconds.
Unfortunately this use case is really better suited to an add-in then a macro, because an add-in can listen to events from the time it is loaded.
Photo of Michael

Michael

  • 3 Posts
  • 0 Reply Likes
Hi Francis, 
Thanks for your reply. Would you by any chance have the code for a macro that is able to listen to onAfter events? I tried that earlier, but could not get it to work.
Thanks.