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
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