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