Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word - beforeSave event

Status
Not open for further replies.

kpal29

Technical User
Feb 8, 2003
147
DK
I have a document template that uses form fields. Some of the fields are required so I need to use the 'BeforeSave' event to open a msgbox advising user to fill in the fields, then do not save the document and exit sub.

I have the following code:

Private Sub appWord_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
Dim pc As DropDown
Set pc = ActiveDocument.FormFields("ProfitCenter").DropDown
If pc.Value = 1 Then
Set Doc = ActiveDocument
MsgBox "You have not filled out some required fields." & Chr(10) & "You cannot" & _
"save your work until you do." & Chr(10) & "Please review and complete the form before saving."
SaveAsUI = False
Cancel = True
End If
End Sub

I have tried to follow the steps in the help titled 'Using Events with the Application Object' but cannot get this to kick off on the 'save' event.
 
Hi kpal29,

Instead of trying to use Application Events, you could just intercept the Save/SaveAs commands.

In your template create two Subs, FileSave and FileSaveAs, and in those, do your checking before either outputting your message box or invoking the Save proper, something like this:
Code:
[blue]Sub FileSaveAs()
    If Happy Then [green]' your checks here[/green]
        Application.Dialogs(wdDialogFileSaveAs).Show
    Else
        Msgbox etc.
    End If
End Sub[/blue]
.. and something similar for FileSave

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
What event would evoke this code? The checking needs to be done at the time the user is trying to save a copy of the template.
 
It's not event code.

Calling a Sub the same name as a Word command means that the Sub will get executed in preference to the Word command of the sam ename, so "Sub FileSave" replaces the Word Save command; it will run when the user clicks on the Save icon or selects File > Save from the Menu. To catch everything you need to trap both FileSave and FileSaveAs.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
User should not be trying to save a copy of the template. Do you mean save the cloned document created from the template?

Gerry
 
Thanks! This works great! 3 stars for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top