I have some strange behaviour from an event handling function for closing a word document. I'm using Word's DocumentBeforeClose event handler in my Outlook module. In the handler, I prompt the user with a message that asks do they want to finalise the document, discard the document, or keep editing.
If I use the MsgBox function with vbYesNoCancel buttons - then the event handler fires every time I close the Word document. This works as expected.
If I use a custom user form with three buttons ("Finalise", "Discard", "Continue Editing"), then the event handler only fires the first time the Word document is closed. If the user clicks Continue Editing, then the next time they close the document, the events handler isn't fired.
I don't understand why these two cases cause different behaviour? Why is the events handler cancelled if I use my custom user form?
Events Handler Class (Not working version)
Option Explicit
Private WithEvents mWordApp As word.Application
Private Sub mWordApp_DocumentBeforeClose(ByVal Doc As document, Cancel As Boolean)
Dim msgBoxResponse As String
'This code brings Outlook back to the active window so the user can response to the form
AppActivate Application.ActiveExplorer.Caption
SendKeys "%"
Set finaliseUserForm = New UserFormFinaliseRFI
finaliseUserForm.show
msgBoxResponse = finaliseUserForm.response
Unload finaliseUserForm
Set finaliseUserForm = Nothing
'msgBoxResponse = MsgBox("Do you want to finalise the document?", vbYesNoCancel)
If msgBoxResponse = "Finalise" Then
'If msgBoxResponse = vbYes Then
Set mWordApp = Nothing
Else
Cancel = True
AppActivate "test.docx"
End If
End Sub
Public Sub StartEvents()
Set mWordApp = CreateObject("Word.Application")
End Sub
Public Sub OpenWordDocument(filePath As String)
mWordApp.Documents.Open filePath
mWordApp.Visible = True
End Sub
Events Handler Class (Working version)
Option Explicit
Private WithEvents mWordApp As word.Application
Private Sub mWordApp_DocumentBeforeClose(ByVal Doc As document, Cancel As Boolean)
Dim msgBoxResponse As String
'This code brings Outlook back to the active window so the user can response to the form
AppActivate Application.ActiveExplorer.Caption
SendKeys "%"
'Set finaliseUserForm = New UserFormFinaliseRFI
'finaliseUserForm.show
'msgBoxResponse = finaliseUserForm.response
'Unload finaliseUserForm
'Set finaliseUserForm = Nothing
msgBoxResponse = MsgBox("Do you want to finalise the document?", vbYesNoCancel)
'If msgBoxResponse = "Finalise" Then
If msgBoxResponse = vbYes Then
Set mWordApp = Nothing
Else
Cancel = True
AppActivate "test.docx"
End If
End Sub
Public Sub StartEvents()
Set mWordApp = CreateObject("Word.Application")
End Sub
Public Sub OpenWordDocument(filePath As String)
mWordApp.Documents.Open filePath
mWordApp.Visible = True
End Sub
Test Module Sub
Option Explicit
Private mEvents As WordEventsHelper
Public Sub testEvents()
Set mEvents = New WordEventsHelper
mEvents.StartEvents
mEvents.OpenWordDocument "\(mypath)\test.docx"
AppActivate "test.docx"
End Sub
User Form Code
Private mResponse As String
Public Property Get response() As String
response = mResponse
End Property
Private Sub CommandButtonFinalise_Click()
mResponse = "Finalise"
Me.Hide
End Sub
Private Sub CommandButtonDiscard_Click()
mResponse = "Discard"
Me.Hide
End Sub
Private Sub CommandButtonContinueEditing_Click()
mResponse = "Continue Editing"
Me.Hide
End Sub