简体   繁体   English

VBA:全局变量在“Workbook_Open”子后清除

[英]VBA: Global Variable Cleared after 'Workbook_Open' sub

I have set up an application level event class to monitor when new workbooks are created/ opened by following CPearson's guide . 我已经设置了一个应用程序级事件类来监视CPearson指南创建/打开新工作簿的时间 This works fine in isolation. 这在隔离工作正常。 However, it is intended as part of add-in I'm writing where several other subs are also called in the 'Workbook_Open' sub, see below code: 但是,它是作为加载项的一部分而写的,我在写'Workbook_Open'子中也调用了其他几个子,见下面的代码:

Private XLApp As baseCXlEvents

Private Sub Workbook_Open()
    Set XLApp = New baseCXlEvents
    test
    AddLocalReferences
    AddModules
    AddClassModules
    Debug.Print "testing"
End Sub

So the XLApp variable is called in the module scope as a baseCXlEvents class. 因此, XLApp变量在模块范围内作为baseCXlEventsbaseCXlEvents I have added a Class_Terminate event to this class and this is triggered after the Debug.print "testing" is run, ie XLApp is terminated after the Workbook_Open sub has run. 我已经向这个类添加了一个Class_Terminate事件,这是在运行Debug.print "testing"之后触发的,即在Workbook_Open子运行后终止XLApp This does not happen when I quote out the subs AddLocalReferences , AddModules , and AddClassModules , which do exactly as their names would imply. 当我引用子AddLocalReferencesAddModulesAddClassModules ,就不会发生这种情况,这与其名称所暗示的完全相同。 The sub test only prints a messages in debug to test whether calling additional subs caused XLApp to be terminated. test仅在调试中打印消息,以测试是否调用其他子设备导致XLApp终止。

My current 'hunch' is that adding references, modules, or class modules counts as "editing", causing it to be terminated, as explained in this MS Support document . 我目前的“预感”是添加引用,模块或类模块计为“编辑”,导致它被终止,如本MS支持文档中所述 But, if so, why doesn't XLApp get terminated until the end of the sub? 但是,如果是这样,为什么XLApp在子结束之前不会被终止? As opposed to as soon as AddLocalReferences is run. AddLocalReferences运行时相反。

Any suggestions why the class is terminated? 有关课程终止的任何建议吗? I need it to 'stay alive' and also need to load additional modules and references for the addin upon workbook_open. 我需要它“保持活力”,还需要在workbook_open上加载额外的模块和引用。 If needed more details of this code can be provided. 如果需要,可以提供此代码的更多详细信息。

I've decided to add my baseCXlEvents class module's code : 我已经决定添加我的baseCXlEvents类模块的代码

Option Explicit

Private WithEvents App As Application

Private Sub App_NewWorkbook(ByVal Wb As Workbook)
    MsgBox "New Workbook: " & Wb.Name
End Sub

Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
    MsgBox "Workbook opened: " & Wb.Name
End Sub

Private Sub Class_Initialize()
    Debug.Print "Initializing baseCXlEvents instance.."
    Set App = Application
End Sub

Private Sub Class_Terminate()
    Debug.Print "Terminating baseCXlEvents instance.."
End Sub

Well then try to separate the Workbook_Open event handler stuff (where you add references etc.) from the creation of the instance of the class baseCXlEvents using Auto_Open . 然后尝试使用Auto_Open从创建类baseCXlEvents的实例中分离Workbook_Open事件处理程序的东西(在那里添加引用等)。

Workbook_Open runs first, Auto_Open runs then. 首先运行Workbook_Open ,然后运行Auto_Open

Note: baseCXlEvents Instancing must be set to Public. 注意: baseCXlEvents Instancing必须设置为Public。

在此输入图像描述

ThisWorkbook class ThisWorkbook类

Public XLApp As baseCXlEvents

Private Sub Workbook_Open()

    Test
    AddLocalReferences
    AddModules
    AddClassModules

End Sub

Standard module 标准模块

Sub Auto_Open()
   Set ThisWorkbook.XLApp = New baseCXlEvents
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM