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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnSaveEvent

Status
Not open for further replies.

into

Technical User
Dec 16, 2002
17
Is there any way to do something juust before the user saves the document?

/Torfi
 
Excel has a Workbook_BeforeSave Event. Which application are you talking about?

Cheers,

Roel
 
Word...

Sorry i did'nt mention it.

/Torfi
 
hmm, I'd expected to find a beforesave event in a word-document as well, but unfortunately not.

I'm sorry, but I don't know enough about Word to help you.

Cheers,

Roel
 
Hi there,

There is no native event in Word for this, so you must create your own using a Class module. Here is what you need to do...

Open the VBE (Alt + F11)
Press Ctrl + R
Select your document on the left (PE)
Click Insert | Class Module
Paste the following code there...

Code:
Option Explicit

Public WithEvents appWD As Application

Private Sub Class_Initialize()
    Set appWD = Word.Application
End Sub

Private Sub appWD_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
    If MsgBox("Do you want to save?", vbYesNo, "Save?") = vbYes Then Exit Sub
    SaveAsUI = False
    Cancel = True
End Sub

NOTE: You must select the new class module on the left and hit F4 and change the name to Class_App!

Now double click the ThisDocument module and paste this code (assuming you don't have any other document_open events)...

Code:
Option Explicit

Dim wdDoc As New Class_App

Private Sub Document_Open()
    Set wdDoc.appWD = Word.Application
End Sub

Save the document, close it, then reopen it. Note that before you close the security settings (Tools | Macro | Security) must be at Medium or lower for this to work.

HTH

-----------
Regards,
Zack Barresse

Simplicity is the ultimate sophistication.
- Leonardo da Vinci
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top