Class Module for Save Events

  • Thread starter Montana DOJ Help Desk
  • Start date
M

Montana DOJ Help Desk

Word 2000

I've been experimenting with my first class module. Here's what I have:

Class Module
==========
Public WithEvents App As Word.Application

Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As
Boolean, Cancel As Boolean)
If ActiveDocument.Name = "Locates.doc" Then SaveDocument
End Sub

Private Sub SaveDocument()
ThisDocument.SaveFile
End Sub


Standard Module
============
Dim X As New clsSaveEvent

Private Sub Document_Open()
InitializeSaveEvent
End Sub

Sub InitializeSaveEvent()
Set X.App = Word.Application
End Sub

Sub SaveFile()
< Some code >
End Sub


So the event gets initialized when the document is opened. Each time a save
event occurs the code in the class module runs, and if the document name is
"Locates.doc" then SaveDocument, and finally SaveFile are called. This
seems to work rather well, but being a class module newbie, I have several
questions. I hope no one minds that I'm piling all these questions into one
message, but they are all related to the same issue, so I figured that it
would be easier this way.

1) The event works at the application level, and I want it to run only in a
specific document, which is why I have the App_DocumentBeforeSave routine
testing for the document name. I tried putting

Set X.App = Word.ActiveDocument

in the InitializeSaveEvent routine with

Public WithEvents App As Word.Document

in the class module. No errors occur with these lines in place, but at the
same time, nothing appears to happen when I save the document. Is there a
way to make this work only at the document level?

2) The event gets initialized when the document is opened, but I've noticed
that after changing code in the module, I often need to initialize the event
again. It makes sense that if the code in the event is changed, you'd have
to reinitialize the event. However, is it possible that other changes, such
as changes to the document or template, might also require the event to be
reinitialized?

3) My experience with forms in Access, Excel, and Word have taught me that
it's easy to end up with looping code when using before and after events,
and that makes me a little leery here. What are the drawbacks, if any, of
using the App_DocumentBeforeSave?

4) While the code above basically works, there is an annoyance that I have
yet to solve. Say that the user does a Save As. The code will run as
expected, but when the code completes the Save As dialog box will be
displayed. I don't want that because I'm trying to control how the document
gets saved, and I don't want the user to be able to change the name or the
location of the file. Is there a way to prevent the Save As dialog box from
being displayed (I read in the Help that the return value for the Cancel
button is 0, and I thought maybe that could somehow be used to suppress the
Save As dialog box, but I'm not sure how that would work)?

5) This is somewhat related to question 4. Is there a way to disable the
Save As option in a specific document (not application-wide)?


Any information that you may be able to provide will be greatly appreciated.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
A

Alex Ivanov

I think there may be a better way to do what you want. In a document or a
template, in any module declare two subs called "Save" and "SaveAs" that
accept no parameters. These subs will be fired when a user selects an
appropriate action instead of standard action Word would perform otherwise.
Warning: If you put the code into a normal template it will run if the user
tries to save any document; If you really want that, make sure your subs are
calling the default procedures if an active document is NOT what you need to
deal with.
 
M

Montana DOJ Help Desk

Yeah, I know that I could do it that way. But I wanted to experiment with
class modules, so I wanted to see if there is a way to intercept ALL save
operations without the need for separate routines.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
A

Alex Ivanov

Sorry for misleading. Sub names should be "FileSave" and "FileSaveAs" not
"Save" and "SaveAs"
I believe they can be in any module, not necessarily in a class module. If
these subs are in scope of an active document, they will suppress execution
of built in commands with the same name, no matter how the command is
executed. You may click the little diskette icon on the toolbar, or click
File/Save from menu, your FileSave macro will run, not the built-in command.

If you put these macros into a document, only that document will be
affected. If you put it into a template, all documents based on that
template will be affected. If you have it in any global template that is
currently loaded, including Normal, all documents will be affected.

Try to put the following code into a normal template and you won't be able
to save any documents.

Public Sub FileSave()
MsgBox "I don't want to save this junk!"
End Sub

Public Sub FileSaveAs()
MsgBox "Do you really mean 'Save'?"
End Sub

I believe you can override any built-in Word command this way, but there are
too many of them to experiment.

HTH
 

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