vba Workbook_Open() 不会在 excel 已经运行的情况下执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9006902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:03:38  来源:igfitidea点击:

Workbook_Open() is not executed with excel already running

excelexcel-vbaexcel-2007vba

提问by Thumper

using excel 2007 I programmed a macro, which I would like to start with the Workbook_Open() call (placed in "thisWorkbook").

使用 excel 2007 我编写了一个宏,我想从 Workbook_Open() 调用(放置在“thisWorkbook”中)开始。

This works fine as long as this is a new "excel-session", therefore it is started together with the xlsm-file / I start excel and load the file.

只要这是一个新的“excel 会话”,它就可以正常工作,因此它与 xlsm 文件一起启动/我启动 excel 并加载文件。

However as soon as excel is already running, the Workbook_Open() function is not executed. The macro otherwise still works fine, as soon as I start it manually after the workbook has been loaded.

但是,一旦 excel 已经运行,Workbook_Open() 函数就不会执行。宏在其他方面仍然可以正常工作,只要我在加载工作簿后手动启动它。

To clarify: This happens even if no other workbook is open, just excel, so I am convinced that no other calculation could interfere with the Workbook_Open() call (as proposed most of the time).

澄清一下:即使没有其他工作簿打开,也会发生这种情况,只是 excel,所以我相信没有其他计算会干扰 Workbook_Open() 调用(大多数情况下都建议这样做)。

(the problem seems to exist for excel 2003 too: Excel Workbook Open Event macro doesn't always run)

(这个问题似乎也存在于 excel 2003 中: Excel 工作簿打开事件宏并不总是运行

I would be grateful for any hints how to call my macro in any case! (As my users most of the time already have excel up and running when starting my file) Thanks

我将不胜感激任何提示如何在任何情况下调用我的宏!(因为我的用户大部分时间在启动我的文件时已经启动并运行了 excel)谢谢

回答by Bruno Leite

You can create a global bool variable, and set a value with true in workbook_open .

您可以创建一个全局 bool 变量,并在 workbook_open 中设置一个值为 true 。

And after you verify value of variable

在你验证变量的值之后

'In Module
Global BoolCheck As Boolean


Sub CheckValue()
'Verify if Workbook_Open Event is called
If BoolCheck = False Then
    'case not call the
    ActiveWorkbook.Workbook_Open
End If
End Sub

'in Workbook Object
Public Sub Workbook_Open()
    BoolCheck = True
End Sub

[]′s

[]的