【发布时间】:2022-10-06 13:19:19
【问题描述】:
我在关闭 word 文档的事件处理函数中有一些奇怪的行为。我在 Outlook 模块中使用 Word 的 DocumentBeforeClose 事件处理程序。在处理程序中,我用一条消息提示用户,询问他们是要完成文档、丢弃文档还是继续编辑。
如果我将 MsgBox 函数与 vbYesNoCancel 按钮一起使用,那么每次关闭 Word 文档时都会触发事件处理程序。这按预期工作。
如果我使用带有三个按钮(\"Finalise\"、\"Discard\"、\"Continue Editing\")的自定义用户表单,则事件处理程序仅在 Word 文档第一次关闭时触发。如果用户单击继续编辑,那么下次他们关闭文档时,不会触发事件处理程序。
我不明白为什么这两种情况会导致不同的行为?如果我使用自定义用户表单,为什么会取消事件处理程序?
事件处理程序类(非工作版本)
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
事件处理程序类(工作版)
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
测试模块子
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
用户表单代码
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
-
您是否尝试调试代码?您是否使用用户表单获得正确的结果?
-
是的,一切似乎都正常工作 - 包括用户表单。除了事件处理程序仅在使用自定义用户表单时第一次触发的事实。
-
确保将
Cancel参数设置为 true 以再次触发事件。 -
我在事件处理函数的 \'else\' 部分将 Cancel 设置为 true。无论是我的客户用户表单还是 msgbox,它都能正确执行。只是当我使用用户表单时,处理程序再也不会触发。
-
尝试使用
Unload方法而不是Hide。
标签: vba outlook ms-word office-automation