Share via

Run Document Events from a Class Module

Anonymous
2024-03-03T16:10:18+00:00

Hi All,

Please could I have a little help with class modules and events.

Setting up Application level events is fine - got that working ok.

What I would like to do is have a "template add-in" dotm file in the Word STARTUP folder that runs Document events on all open documents - i.e. on the ActiveDocument.

I have made a class module cEventClass which contains:

Public WithEvents WdAppEvents As Application 

Public WithEvents WdDocEvents As Document

I then have code in a normal code module that will run when Word starts (e.g. from the ribbon's OnLoad event) which will initiate the event class object:

Public wdEvents As New cEventClass

Sub Start()
    Set wdEvents.WdAppEvents = Application
    Set wdEvents.WdDocEvents = ActiveDocument
End Sub

Back in the class module, the Application Events work fine, and I thought I could use the WindowActivate event to set the document events to the new ActiveDocument:

Private Sub WdAppEvents_WindowActivate(ByVal Doc As Document, ByVal Wn As Window) 

    Set wdEvents.WdDocEvents = Doc 

    Debug.Print "Activated " & Doc.name 

End Sub

This code is running when I switch between open documents, as I see the comment in the Immediate window.

But none of the Document events are firing. e.g., also in cEventClass class module:

Private Sub WdDocEvent_ContentControlOnEnter(ByVal ContentControl As ContentControl) 

    Debug.Print "Entered " & ContentControl.Title 

End Sub 

Private Sub WdDocEvent_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) 

    Debug.Print "Exited " & ContentControl.Title 

End Sub

These do not run. Is there a way to make the Document events fire from a global add-in?

Cheers
Rich

Microsoft 365 and Office | Word | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2024-03-03T16:35:29+00:00

Think I've fixed it!

For future reference:

In first class module cAppEventClass:

Public WithEvents wdAppEvents As Application 

Private Sub WdAppEvents_WindowActivate(ByVal Doc As Document, ByVal Wn As Window) 

    Set wdDoc.wdDocEvents = Doc 

    Debug.Print "Activated " & Doc.name 

End Sub

In second class module cDocEventClass:

Public WithEvents wdDocEvents As Word.Document 

Private Sub wdDocEvents_ContentControlOnEnter(ByVal ContentControl As ContentControl) 

    Debug.Print Now 

End Sub 

Private Sub wdDocEvents_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) 

    Debug.Print Now 

End Sub

In a normal code module, and set up to run when Word starts, e.g. via ribbon OnLoad event, or AutoExec procedure:

Public wdApp As New cAppEventClass 

Public wdDoc As New cDocEventClass 

Sub AutoExec() 

    Set wdApp.wdAppEvents = Application 

'    Set wdDoc.wdDocEvents = ActiveDocument   'Do not do this here - there won't be an ActiveDoc when this code runs!

End Sub

Now when I click in and out of a ContentControl in ANY open document, the relevant Document event fires!

Cheers
Rich

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-03-03T20:02:52+00:00

    Thanks Charles

    Was this answer helpful?

    0 comments No comments
  2. Charles Kenyon 167.8K Reputation points Volunteer Moderator
    2024-03-03T19:06:20+00:00

    I've marked your update as an Answer; thank you for sharing.

    See also Working With Events on this page: Word Macros and Visual Basic for Applications (VBA) FAQ

    Was this answer helpful?

    0 comments No comments