Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2006

Tip: Looking for answers? Try searching our database.

Event Hander

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg Maxey - 23 Aug 2006 19:20 GMT
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.
Greg Maxey - 23 Aug 2006 20:33 GMT
Found the problem.

Dim X As New EventsClassModule

needed to be at the module level.

> Jonathan West proposed a "DocumentBeforeSave" event handler as a
> solution to a problem.
[quoted text clipped - 25 lines]
>
> Nothing happens when I open the document and then save it.
Jezebel - 23 Aug 2006 23:39 GMT
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

> Found the problem.
>
[quoted text clipped - 31 lines]
>>
>> Nothing happens when I open the document and then save it.
Greg Maxey - 24 Aug 2006 00:25 GMT
Thanks Jezebel for the explanation and example.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Conceptually, a better way to code this is to put the application
> assignment in the object's Initialize event --
[quoted text clipped - 58 lines]
>>>
>>> Nothing happens when I open the document and then save it.
Greg Maxey - 24 Aug 2006 01:07 GMT
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.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Conceptually, a better way to code this is to put the application
> assignment in the object's Initialize event --
[quoted text clipped - 58 lines]
>>>
>>> Nothing happens when I open the document and then save it.
Jezebel - 24 Aug 2006 01:24 GMT
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.

> Jezebel,
>
[quoted text clipped - 77 lines]
>>>>
>>>> Nothing happens when I open the document and then save it.
Greg Maxey - 24 Aug 2006 01:23 GMT
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?
Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Conceptually, a better way to code this is to put the application
> assignment in the object's Initialize event --
[quoted text clipped - 58 lines]
>>>
>>> Nothing happens when I open the document and then save it.
Jezebel - 24 Aug 2006 01:32 GMT
Yep, that's the way to do it.

> Jezebel,
>
[quoted text clipped - 84 lines]
>>>>
>>>> Nothing happens when I open the document and then save it.
Greg Maxey - 24 Aug 2006 02:15 GMT
Ok thanks.  I will try to remember.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Yep, that's the way to do it.
>
[quoted text clipped - 92 lines]
>>>>>
>>>>> Nothing happens when I open the document and then save it.
Doug Robbins - Word MVP - 23 Aug 2006 21:10 GMT
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.

Signature

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

> Jonathan West proposed a "DocumentBeforeSave" event handler as a
> solution to a problem.
[quoted text clipped - 25 lines]
>
> Nothing happens when I open the document and then save it.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.