Word 全局模板 VBA:ContentControlOnExit 事件在放置在启动文件夹中时未触发
Word Global Template VBA: ContentControlOnExit event not firing when placed in the Startup folder
下面的代码放在 .dotm 模板文件的 ThisDocument 字对象中。直接打开文件时,每次退出活动文档中的内容控件时,都会按预期触发下面的事件。但是,当模板放在 Startup 文件夹中并自动打开时,事件不会触发。
关于如何修改它以使其在“启动”文件夹中按预期工作有任何想法吗?
Private Sub Document_ContentControlOnExit(ByVal ContentControl As _
ContentControl, Cancel As Boolean)
MsgBox ("Fired")
End Sub
我过去也遇到过这个问题。
你不会让它像那样工作,它只会在你加载的“*.dotm”文件本身上触发 ContentControlOnExit
事件的事件。
要对宏文档之外发生的变化做出反应,您必须使用相当复杂的结构,我将向您简要解释一下。
您必须创建一个 Class clsDocument
,其中您有一个变量 Public WithEvents p_Document as Word.Document
。在你的 class 中,你然后监听你的 p_Document
的事件 ContentControlOnExit
并将你的代码放在那里(在你的情况下 MsgBox ("Fired")
.
接下来,您必须收听一般事件 "AutoExec" 和 "AutoOpen" 以及 "Application_DocumentChange"。在所有这些事件中,您基本上只需将全局变量 p_Document
设置为传递给事件的文档或(如果没有文档传递给事件处理程序)活动文档的值。
由于所有这些事件反应都或多或少地做着相同的事情,因此在宏的模块中创建一个新过程,如下所示:
Public g_clsWordDocument As clsDocument
Public Sub SetUpDocumentEvents(Optional ByRef a_Document As Word.Document = Nothing)
If Application.Documents.Count > 0 Then
If a_Document Is Nothing Then Set a_Document = ActiveDocument
Set g_clsDocument = New clsDocument
Set g_clsDocument.p_Document = a_Document
End If
End Sub
然后,在您的 ThisDocument
宏文件中创建以下程序。
Option Explicit
Private g_clsWordApplication As clsApplication
Public Sub AutoExec()
Set g_clsWordApplication = New clsApplication
Set g_clsWordApplication.WordApplication = Word.Application
Call modYourModule.SetUpDocumentEvents
End Sub
Private Sub Document_New()
Call modYourModule.SetUpDocumentEvents
End Sub
Private Sub Document_Open()
Call modYourModule.SetUpDocumentEvents
End Sub
Public Sub AutoOpen()
Call modYourModule.SetUpDocumentEvents
End Sub
这样,您就可以捕获文档事件。正如您在 AutoExec
函数中看到的那样,它与应用程序对象相同,只需创建一个带有 WithEvents WordApplication as Word.Application
的新 clsAplication
即可对事件做出反应。所有这些代码都进入您的 dotm 文件,因此是全局模板文件。
通过这种方式,您可以对您想要的事件做出反应。
虽然我还没有找到另一种方法来解决这个问题,但我仍然不满意它必须如何实现。如果有其他选择可以解决您的问题,我强烈建议您尝试另一种方式。
下面的代码放在 .dotm 模板文件的 ThisDocument 字对象中。直接打开文件时,每次退出活动文档中的内容控件时,都会按预期触发下面的事件。但是,当模板放在 Startup 文件夹中并自动打开时,事件不会触发。
关于如何修改它以使其在“启动”文件夹中按预期工作有任何想法吗?
Private Sub Document_ContentControlOnExit(ByVal ContentControl As _
ContentControl, Cancel As Boolean)
MsgBox ("Fired")
End Sub
我过去也遇到过这个问题。
你不会让它像那样工作,它只会在你加载的“*.dotm”文件本身上触发 ContentControlOnExit
事件的事件。
要对宏文档之外发生的变化做出反应,您必须使用相当复杂的结构,我将向您简要解释一下。
您必须创建一个 Class clsDocument
,其中您有一个变量 Public WithEvents p_Document as Word.Document
。在你的 class 中,你然后监听你的 p_Document
的事件 ContentControlOnExit
并将你的代码放在那里(在你的情况下 MsgBox ("Fired")
.
接下来,您必须收听一般事件 "AutoExec" 和 "AutoOpen" 以及 "Application_DocumentChange"。在所有这些事件中,您基本上只需将全局变量 p_Document
设置为传递给事件的文档或(如果没有文档传递给事件处理程序)活动文档的值。
由于所有这些事件反应都或多或少地做着相同的事情,因此在宏的模块中创建一个新过程,如下所示:
Public g_clsWordDocument As clsDocument
Public Sub SetUpDocumentEvents(Optional ByRef a_Document As Word.Document = Nothing)
If Application.Documents.Count > 0 Then
If a_Document Is Nothing Then Set a_Document = ActiveDocument
Set g_clsDocument = New clsDocument
Set g_clsDocument.p_Document = a_Document
End If
End Sub
然后,在您的 ThisDocument
宏文件中创建以下程序。
Option Explicit
Private g_clsWordApplication As clsApplication
Public Sub AutoExec()
Set g_clsWordApplication = New clsApplication
Set g_clsWordApplication.WordApplication = Word.Application
Call modYourModule.SetUpDocumentEvents
End Sub
Private Sub Document_New()
Call modYourModule.SetUpDocumentEvents
End Sub
Private Sub Document_Open()
Call modYourModule.SetUpDocumentEvents
End Sub
Public Sub AutoOpen()
Call modYourModule.SetUpDocumentEvents
End Sub
这样,您就可以捕获文档事件。正如您在 AutoExec
函数中看到的那样,它与应用程序对象相同,只需创建一个带有 WithEvents WordApplication as Word.Application
的新 clsAplication
即可对事件做出反应。所有这些代码都进入您的 dotm 文件,因此是全局模板文件。
通过这种方式,您可以对您想要的事件做出反应。 虽然我还没有找到另一种方法来解决这个问题,但我仍然不满意它必须如何实现。如果有其他选择可以解决您的问题,我强烈建议您尝试另一种方式。