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

Document_Close() check result of "Do you want to save this doc" dialog

Status
Not open for further replies.

dscs

Programmer
Feb 26, 2005
5
US
I want to be able to check if "Yes" is selected if "Do you want to save this doc" dialog is displayed so that I can do some validation before the document is saved. I can't find how to check for the result of this dialog. Any help would be appreciated.
 
Hi,
you need to activate Application events to manage closing/saving process. See the help file for details, the startup, ThisDocument module:
Code:
Public WithEvents wdApp As Word.Application

Private Sub Document_Open()
Set wdApp = Application
End Sub

Private Sub wdApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
If Doc Is ThisDocument Then
    '
End If
End Sub

Private Sub wdApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If Doc Is ThisDocument Then
    '
End If
End Sub

combo
 
I am still having trouble with this. I have this setup as a Template so when I double click on the template to create a new document, it has it's own "ThisDocument". Is this correct?

The Template has a Validation Module (Macro) that I run in FileSave and in FileSaveAs. If there are no Errors it continues through the code, if there are any Errors, it skips to the bottom of the code and doesn't save the document. I am trying to make sure that the Validation Module is ran if the document is Closed and the Dialog for "Do you want to Save changes to document" Yes, No, Cancel - with the user selecting Yes.

Am I missing something here?
 
Hi,
ThisDocument module, in regular document, is used to handle specific document events. In case of a template, event macros run for documents that use this template.
ThisDocument, as Document type object, also means a word document, where the referencing code is.

Document_Close procedure offers limited power to handle all the process. The application level events are much more convinient: aside of returning a reference to the document they offer more events and options, some are reported 'before' action. This approach requires more programming, but can benefit by customisation of some actions, not accesible in other way.
In your case (all code in the template), you need a class module for tracking application events and automatic procedure that instantiates the class. After hooking the required event, it can be handled programmatically, with your own messages.
Tto switch initial code: if you search word help for auto macros, you can find description of special 'AutoExec' procedure in 'Main' named module. That can be used to initialise the class and assign WithEvents application variable.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top