Excel 控件事件在应用程序关闭时触发

发布于 2024-12-28 19:23:51 字数 199 浏览 0 评论 0原文

我有一个 Excel 工作簿,工作表上有 ActiveX 控件(而不是用户窗体上!)。当我关闭工作簿(不关闭 Excel)时,一切正常,但是当我完全关闭 Excel 时,它会触发工作表上 ActiveX 组合框的更改事件。这会产生错误,因为 Excel 似乎已经通过删除控件进行了一些清理。

我该如何绕过或解决这个问题?这些事件不应首先触发,因为关闭时不会更改任何值。

I've an Excel-workbook with ActiveX controls on worksheets (not on Userform!). When I close the workbook (without closing Excel) everything works well, but when I close Excel entirely it triggers Change events of ActiveX Comboboxes on the worksheets. This creates errors because it seems that Excel has done some cleaning already by removing controls.

How can I bypass or solve this? These events shouldn't trigger in the first place because no values are changed on close.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少钕鈤記 2025-01-04 19:23:51

如果不了解 ActiveX 控件的设置方式,我无法告诉您为什么会触发它们的更改事件。但是,假设工作簿的 BeforeClose 事件在 ActiveX 更改事件之前触发,您可以添加一个全局变量来跟踪工作簿何时关闭。

在代码模块中:

Public g_bClosingWorkbook As Boolean

在 ThisWorkbook 中:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
   g_bClosingWorkbook = True
End Sub

Private Sub Workbook_Open()
   g_bClosingWorkbook = False
End Sub

如果不关闭工作簿,则仅运行更改事件。

Private Sub ComboBox1_Change()
   If Not g_bClosingWorkbook Then
      ' do stuff here
   End If
End Sub

Without seeing how your ActiveX controls are setup I can't tell you why their change events are firing. However, assuming the workbook's BeforeClose event fires before the ActiveX change events, you can add a global variable to track when the workbook is closing.

In a code module:

Public g_bClosingWorkbook As Boolean

In ThisWorkbook:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
   g_bClosingWorkbook = True
End Sub

Private Sub Workbook_Open()
   g_bClosingWorkbook = False
End Sub

Then only run the change events if not closing the workbook.

Private Sub ComboBox1_Change()
   If Not g_bClosingWorkbook Then
      ' do stuff here
   End If
End Sub
诗笺 2025-01-04 19:23:51

将此子项插入模块中,并使用它来保存和关闭工作簿和 Excel。首先禁用事件,这应该可以防止出现问题。

Sub closeNoEvents()
   Application.EnableEvents = False
    On Error Resume Next
    ThisWorkbook.Save
    Application.Quit
    ThisWorkbook.Close
End Sub

您还可以检查“ThisWorkbook”中的代码;也许Workbook_BeforeClose中有一些东西。

Insert this sub into a module and use it to save and close both the workbook and Excel. Events are disabled first which should prevent the problem.

Sub closeNoEvents()
   Application.EnableEvents = False
    On Error Resume Next
    ThisWorkbook.Save
    Application.Quit
    ThisWorkbook.Close
End Sub

You also might check to see what code is in "ThisWorkbook"; maybe there is something in Workbook_BeforeClose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文