DocumentBeforeSave function just for one file in a group

J

Jeffrey Grantz

I have been trying to set up what I thought would be an easy VBA macro for
a Word document. I am using Word 2003. I have a file that has a date/time
stamp line at the beginning of the file showing when that file was last
saved. I wanted to automate this for JUST THIS FILE. The code below is
located only in the project area for this particular Word document, not in
the normal.dot. The code works for the file, but it also affects other
files that are open at the same time. For example, if I have my file open
and open another file my date/time line is added to it as well. If I open a
new message in my outlook, the line will be added to the first line of the
e-mail (I use Word as the editor for Outlook). How can I limit this
function to just this file even when I am doing SaveAs (I know I can't test
for the file name since SaveAs changes the name)?



Thanks for any help.





In the "ThisDocument" module for the specific document is



Option Explicit



Private Sub Document_Open()

CreateEventHandler

End Sub

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = =
In a module called "modHandleEvents" for the specific document is



' declare type of object to be stored in the objEventHandler variable

Dim objEventHandler As clsEventHandler

Sub CreateEventHandler()

'create a new event handler object and assign it to objEventHandler

Set objEventHandler = New clsEventHandler

Set objEventHandler.AppEventHandler = Word.Application

End Sub



Sub DestroyEventHandler()

'release the memory being used by the event handler object

Set objEventHandler = Nothing

End Sub



= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = =
And in the class module "clsEventHandler" for the specific document is the
code



'Declare that only an instance of Word can be assigned to this variable

'and also that the assigned instance should be notified to

'check inside the event handler for application event procedures.

Public WithEvents AppEventHandler As Word.Application



Private Sub AppEventHandler_DocumentBeforeSave(ByVal Doc As Document,
SaveAsUI As Boolean, Cancel As Boolean)



'My code for the specific file goes here



End Sub
 
J

Jean-Guy Marcil

Jeffrey Grantz was telling us:
Jeffrey Grantz nous racontait que :
I have been trying to set up what I thought would be an easy VBA
macro for a Word document. I am using Word 2003. I have a file that
has a date/time stamp line at the beginning of the file showing when
that file was last saved. I wanted to automate this for JUST THIS
FILE. The code below is located only in the project area for this
particular Word document, not in the normal.dot. The code works for
the file, but it also affects other files that are open at the same
time. For example, if I have my file open and open another file my
date/time line is added to it as well. If I open a new message in my
outlook, the line will be added to the first line of the e-mail (I
use Word as the editor for Outlook). How can I limit this function
to just this file even when I am doing SaveAs (I know I can't test
for the file name since SaveAs changes the name)?


Thanks for any help.





In the "ThisDocument" module for the specific document is



Option Explicit



Private Sub Document_Open()

CreateEventHandler

End Sub

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = =
In a module called "modHandleEvents" for the specific document is



' declare type of object to be stored in the objEventHandler variable

Dim objEventHandler As clsEventHandler

Sub CreateEventHandler()

'create a new event handler object and assign it to objEventHandler

Set objEventHandler = New clsEventHandler

Set objEventHandler.AppEventHandler = Word.Application

End Sub



Sub DestroyEventHandler()

'release the memory being used by the event handler object

Set objEventHandler = Nothing

End Sub



= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = =
And in the class module "clsEventHandler" for the specific document
is the code



'Declare that only an instance of Word can be assigned to this
variable
'and also that the assigned instance should be notified to

'check inside the event handler for application event procedures.

Public WithEvents AppEventHandler As Word.Application



Private Sub AppEventHandler_DocumentBeforeSave(ByVal Doc As Document,
SaveAsUI As Boolean, Cancel As Boolean)
Have you tried with somehting like:
If Doc.Name = said:
'My code for the specific file goes here End If


End Sub

The point is that event handlers have a scope that is Application wide (Set
objEventHandler.AppEventHandler = Word.Application), so you have to specify
in your code that you want it to act only for a specific document.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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