Bonjour,
Dans son message, < Alder > écrivait :
In this message, < Alder > wrote:
|| I have some code in a Document_Close procedure that hides all but one
|| button in a custom commandbar whenever the document is closed. Works
|| like a charm, except when a user selects "Cancel" from the "Are you
|| sure" dialog. In this case, the code fires even though the document is
|| not actually closed.
||
|| How can I delay the triggering of the code until the document is
|| actually closed?
||
You have to intercept theBeforeClose event to test if the document is ready
to be closed or not, if not, cancel the close event and do not initiate your
code. To do that you need the following (Note that module name are very
important. Try it with my name and step through the code to get familiar.
Once you see how it works, you can use names you want):
A standard module called : "WordEventModule"
Put the following code in this standard module:
'_______________________________________
Public MyWordEvent As New WordEventHandler
'_______________________________________
Public Sub RegisterEH()
' Register the Event Handler
Set MyWordEvent.WordApp = Word.Application
End Sub
'_______________________________________
A class module called: "WordEventHandler"
Put the following code in this class module:
'_______________________________________
Public WithEvents WordApp As Word.Application
'_______________________________________
Private Sub WordApp_DocumentBeforeClose(ByVal _
docClosing As Word.Document, _
ByRef Cancel As Boolean)
Dim CheckFirst As Long
With ActiveDocument
If Not .Saved Then
'Yes = 6
'No = 7
'Cancel = 2
CheckFirst = MsgBox("Do you want to save your " _
& "document before closing it?", _
vbInformation + vbYesNoCancel, _
"Document not saved")
Select Case CheckFirst
Case 6
.Save
Case 7
.Saved = True
Case 2
Cancel = True
End Select
End If
End With
End Sub
'_______________________________________
And finally, you have to register the Before close event before it can be
active. TO do that, put the following code in the ThisDocument module:
'_______________________________________
Private Sub Document_New()
WordEventModule.RegisterEH
End Sub
'_______________________________________
'_______________________________________
Private Sub Document_Open()
WordEventModule.RegisterEH
End Sub
'_______________________________________
So, whenever a document is opened or created from the template, the
BeforeClose event will be registered and active.
Whenever you close a document, the BeforeClose event checks if it has been
saved, if not, it asks the user what to do. If the user Cancels, the
BeforeClose event is cancelled as well, therefore stopping the closing
altogether, and preventing you Document_Close event form firing at all.
You can use other events. With your cursor in the
"WordApp_DocumentBeforeClose" sub, drop the list at the top of the code
window, on the right.
It is a shame that these events are not readily available as they are in
Excel.... but, at least you can "create" them.
Good luck!
--
Salut!
______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org