Event Hander

G

Greg Maxey

Jonathan West proposed a "DocumentBeforeSave" event handler as a
solution to a problem.

I have never messed with this sort of thing. I went in help and put
together the following code, but it doesn't seem to work. Can you
advise on what I am missing or doing wrong. Thanks.

This code is in a module in the Document Project:

Sub AutoOpen()
Dim X As New EventsClassModule
Set X.appWord = Word.Application
End Sub

This code is in a Class module name EventsClassModule

Option Explicit
Public WithEvents appWord As Word.Application
Private Sub appWord_DocumentBeforeSave _
(ByVal Doc As Document, _
SaveAsUI As Boolean, _
Cancel As Boolean)
Dim intResponse As Integer
intResponse = MsgBox("Do you really want to " _
& "save the document?", vbYesNo)
If intResponse = vbNo Then Cancel = True
End Sub

Nothing happens when I open the document and then save it.
 
G

Greg Maxey

Found the problem.

Dim X As New EventsClassModule

needed to be at the module level.
 
D

Doug Robbins - Word MVP

Hi Greg,

Another couple of traps to look out for:

The autoexec will not fire if:

Word is selected as the email editor in Outlook and Outlook is started
before Word

Word is started by double clicking on a file in Windows Explorer.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jezebel

Conceptually, a better way to code this is to put the application assignment
in the object's Initialize event --

Private WithEvents mWordApp

Private Sub Class_Initialize()
Set mWordApp = Word.Application
End Sub

Private Sub mWordApp_DocumentBeforeSave() ..


In this particular case it makes no difference to your overall project; but
the principle is that class objects should be self-contained as far as
possible: the calling code shouldn't need to know the details of
initialising the object. It also means you can keep the application
reference private -- you maybe need to make it public for other reasons, but
if not, private makes for more robust code.

It's also unwise to declare class modules using 'as new'. Consider this
sequence --

Dim X As New EventsClassModule
:
Set X = nothing
If X is nothing then .... 'This line will create a new instance of
the object
 
G

Greg Maxey

Jezebel,

I obviously just can't grasp class modules.

I put this in the class module and nothing happens when I try to save:

Option Explicit
Private WithEvents mWordApp As Word.Appilication
Private Sub Class_Initialize()
Set mWordApp = Word.Application
End Sub
Private Sub mWordApp_DocumentBeforeSave()
MsgBox "Test"
End Sub

What am I supposed to code in the project module to enable this to work?

Thanks.
 
G

Greg Maxey

Jezebel,

I have scratched together something that seems to work, but not sure if I
understand your comments about "NEW"

If have this is the project module:

Option Explicit
Dim X As myClass
Sub CallClass()
Set X = New myClass
End Sub

And this in the class module:

Option Explicit
Private WithEvents mWordApp As Word.Application
Private Sub Class_Initialize()
Set mWordApp = Word.Application
End Sub
Private Sub mWordApp_DocumentBeforeSave(ByVal Doc As Document, _
SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "Test"
End Sub

Is this correct?
 
J

Jezebel

Yout still have to instantiate the class object in your AutoNew or
wherever --

Ordinary module --

Dim mclsEvents as EventsClassModule
:
Sub SomethingOrOther()
Set mclsEvents = new EventsClassModule
End Sub


Bear in mind that the object gets zapped whenever your project is reset,
which includes whenever you edit any code.
 
J

Jezebel

Yep, that's the way to do it.



Greg Maxey said:
Jezebel,

I have scratched together something that seems to work, but not sure if I
understand your comments about "NEW"

If have this is the project module:

Option Explicit
Dim X As myClass
Sub CallClass()
Set X = New myClass
End Sub

And this in the class module:

Option Explicit
Private WithEvents mWordApp As Word.Application
Private Sub Class_Initialize()
Set mWordApp = Word.Application
End Sub
Private Sub mWordApp_DocumentBeforeSave(ByVal Doc As Document, _
SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "Test"
End Sub

Is this correct?
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top