1

I really don't know the syntax of the DocumentBeforeClose event. Following this page, I should create a class module called 'EventClassModule' (see also this article). So I did. Then I copied this piece of code (from the example of the first link) into that (class)module:

Public WithEvents appWord as Word.Application 

Private Sub appWord_DocumentBeforeClose _ 
        (ByVal Doc As Document, _ 
        Cancel As Boolean) 

    Dim intResponse As Integer 

    intResponse = MsgBox("Do you really " _ 
        & "want to close the document?", _ 
        vbYesNo) 

    If intResponse = vbNo Then Cancel = True 
End Sub

And finally I put this in a normal module, and executed it:

Dim X As New EventClassModule 
Sub Register_Event_Handler() 
 Set X.App = Word.Application 
End Sub

What does the 'X' means in this case, and what am I doing wrong? There is no event executed when I close the document now.

jroeleveld
  • 456
  • 4
  • 12
  • 22

2 Answers2

2

X is an instance of the class you created (EventClassModule)

Your problem is that .App is not a property of EventClassModule. Change

Set X.App = Word.Application 

to the property you defined in your class

Set X.appWord = Word.Application 
chris neilsen
  • 52,446
  • 10
  • 84
  • 123
1

I tried the same thing! Actually it works for me. I have this in a class module named EventClassModule

Public WithEvents appWord As Word.Application

Private Sub appWord_DocumentBeforeClose _
        (ByVal Doc As Document, _
        Cancel As Boolean)

'Here is the code you want to do before it close
MsgBox "WORKING!"

End Sub

And in a module (not a class module) I have this

Dim X As New EventClassModule

Sub AutoExec()

    'Call any other sub or function you want

    Call Register_Event_Handler

End Sub

Sub Register_Event_Handler()

    Set X.appWord = Word.Application

End Sub

AutoExec is called as soon as the document is loaded. So it calls the sub Register_Event_Handler to register the object X (which is a object EventClassModule, the class module created). So X will be annonced that the document is about to close

hope it helps!

castors33
  • 477
  • 10
  • 26