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.

DocumentBeforeSave function just for one file in a group

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeffrey Grantz - 17 Aug 2006 20:09 GMT
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
Jean-Guy Marcil - 17 Aug 2006 20:47 GMT
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
[quoted text clipped - 63 lines]
> Private Sub AppEventHandler_DocumentBeforeSave(ByVal Doc As Document,
> SaveAsUI As Boolean, Cancel As Boolean)

Have you tried with somehting like:
   If Doc.Name = <YOur specific document name> Then
       >
       > '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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

 
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.