In VBA how to refer to the actual Word document that triggers an event from within its template code

Viewed 123

I'm working with a class that manipulates several Word documents. My intention is to keep references to the Word documents and have them up to date by calling an update method from the template document event methods Open() and Close(), passing a reference to the opened or closed document, like this:

' inside the template's ThisDocument code:
Private Sub Document_Open()
    Call Session.UpdateOpened(ThisDocument)
End Sub

The problem is that ThisDocument refers to the template document, not the actual document that triggers the event, so I had to change ThisDocument to ActiveDocument:

Private Sub Document_Open()
    Call Session.UpdateOpened(ActiveDocument)
End Sub

This should work provided that the events in the template are called from an active document, but is this always the case? And in any case, the question remains:

Is there a way to refer to the actual document from the template code?

FOLLOW-UP: I've seen this question ThisDocument object reference refers to the same issue. The underlying problem is probably one of design so it might never get a straightforward answer. See the comments below for some useful tips.

0 Answers
Related