简体   繁体   English

Excel 2016 中的 Workbook_Open() 未触发

[英]Workbook_Open() in Excel 2016 not firing

Excel 2016 (or 365) does not seem to fire the Workbook_Open() sub reliably or more precisely, not at all! Excel 2016(或 365)似乎没有可靠地或更准确地触发 Workbook_Open() 子程序,根本没有! The simple event sub简单的事件子

Private Sub Workbook_Open()
    MsgBox "Work book is open"
End Sub

does not seem to work.似乎不起作用。 However, if a workbook is already open and then the workbook containing the above Sub is then opened, it does run as expected.但是,如果工作簿已经打开,然后包含上述 Sub 的工作簿被打开,它会按预期运行。 I notice that unlike Excel 2010, 2016 (365) opens each workbook in its own window, not a workbook window in the Excel application window.我注意到与 Excel 2010 不同,2016 (365) 在其自己的窗口中打开每个工作簿,而不是 Excel 应用程序窗口中的工作簿窗口。 Is this a bug in 2016 and is there a workaround?这是 2016 年的错误吗?是否有解决方法?

I have produced a work around for my own applications and that is call the activation of a worksheet and call my initialization routines from there.我为我自己的应用程序制作了一个变通方法,即调用工作表的激活并从那里调用我的初始化例程。 But a bit "rough" and it would be good to have the Workbook_Open() sub working correctly.但是有点“粗糙”,让 Workbook_Open() 子正常工作会很好。

It is a simple single Sub in the ThisWorkbook module.它是 ThisWorkbook 模块中的一个简单的单个 Sub。 Macros are enabled.宏已启用。 In Excel 2010 it works perfectly, as do two other macros in other workbooks for which I have written macros.在 Excel 2010 中,它可以完美运行,就像我为其编写宏的其他工作簿中的其他两个宏一样。 It is just this one thing with Excel 2016. Given that the Workbook_Open() sub is the gateway to a workbook it seems a little strange that we have to go to a workaround to make it function. Excel 2016 仅此而已。鉴于Workbook_Open() sub 是工作簿的网关,我们必须采取一种解决方法使其正常工作似乎有点奇怪。

Excel 2016 (or 365) does not seem to fire the Workbook_Open() sub reliably or more precisely, not at all! Excel 2016(或365)似乎无法可靠地或更精确地触发Workbook_Open()子级,根本不会! The simple event sub简单事件子

Private Sub Workbook_Open()
    MsgBox "Work book is open"
End Sub

does not seem to work.似乎不起作用。 However, if a workbook is already open and then the workbook containing the above Sub is then opened, it does run as expected.但是,如果一个工作簿已经打开,然后打开了包含上述Sub的工作簿,它确实会按预期运行。 I notice that unlike Excel 2010, 2016 (365) opens each workbook in its own window, not a workbook window in the Excel application window.我注意到,与Excel 2010不同,2016(365)在其自己的窗口中打开每个工作簿,而不是在Excel应用程序窗口中打开工作簿窗口。 Is this a bug in 2016 and is there a workaround?这是2016年的错误,有解决方法吗?

I have produced a work around for my own applications and that is call the activation of a worksheet and call my initialization routines from there.我已经为自己的应用程序进行了变通,即调用工作表并从那里调用初始化例程。 But a bit "rough" and it would be good to have the Workbook_Open() sub working correctly.但是有点“粗糙”,让Workbook_Open()子正确工作会很好。

It is a simple single Sub in the ThisWorkbook module.它是ThisWorkbook模块中的一个简单Sub。 Macros are enabled.启用宏。 In Excel 2010 it works perfectly, as do two other macros in other workbooks for which I have written macros.在Excel 2010中,它可以完美工作,就像我为其编写宏的其他工作簿中的其他两个宏一样。 It is just this one thing with Excel 2016. Given that the Workbook_Open() sub is the gateway to a workbook it seems a little strange that we have to go to a workaround to make it function. Excel 2016就是一件事。鉴于Workbook_Open()子是工作簿的网关,我们不得不采取一种变通方法才能使其工作似乎有些奇怪。

Excel 2016 (or 365) does not seem to fire the Workbook_Open() sub reliably or more precisely, not at all! Excel 2016(或365)似乎无法可靠地或更精确地触发Workbook_Open()子级,根本不会! The simple event sub简单事件子

Private Sub Workbook_Open()
    MsgBox "Work book is open"
End Sub

does not seem to work.似乎不起作用。 However, if a workbook is already open and then the workbook containing the above Sub is then opened, it does run as expected.但是,如果一个工作簿已经打开,然后打开了包含上述Sub的工作簿,它确实会按预期运行。 I notice that unlike Excel 2010, 2016 (365) opens each workbook in its own window, not a workbook window in the Excel application window.我注意到,与Excel 2010不同,2016(365)在其自己的窗口中打开每个工作簿,而不是在Excel应用程序窗口中打开工作簿窗口。 Is this a bug in 2016 and is there a workaround?这是2016年的错误,有解决方法吗?

I have produced a work around for my own applications and that is call the activation of a worksheet and call my initialization routines from there.我已经为自己的应用程序进行了变通,即调用工作表并从那里调用初始化例程。 But a bit "rough" and it would be good to have the Workbook_Open() sub working correctly.但是有点“粗糙”,让Workbook_Open()子正确工作会很好。

It is a simple single Sub in the ThisWorkbook module.它是ThisWorkbook模块中的一个简单Sub。 Macros are enabled.启用宏。 In Excel 2010 it works perfectly, as do two other macros in other workbooks for which I have written macros.在Excel 2010中,它可以完美工作,就像我为其编写宏的其他工作簿中的其他两个宏一样。 It is just this one thing with Excel 2016. Given that the Workbook_Open() sub is the gateway to a workbook it seems a little strange that we have to go to a workaround to make it function. Excel 2016就是一件事。鉴于Workbook_Open()子是工作簿的网关,我们不得不采取一种变通方法才能使其工作似乎有些奇怪。

I have same problem then I found solution after google it: https://www.myonlinetraininghub.com/excel-forum/vba-macros/excel-2016-workbook_open-event-doesnt-trigger我有同样的问题,然后我在谷歌之后找到了解决方案: https : //www.myonlinetraininghub.com/excel-forum/vba-macros/excel-2016-workbook_open-event-doesnt-trigger

Then I also use "Private Sub Workbook_Open()" and "Public Sub Auto_Open()" open in excel 2016 that work fine:然后我还使用在 excel 2016 中打开的“Private Sub Workbook_Open()”和“Public Sub Auto_Open()”工作正常:

Private Sub Workbook_Open()
    CustomStartUp
End Sub

Public Sub Auto_Open()
    CustomStartUp
End Sub


Private Sub CustomStartUp()
    MsgBox "Work book is open"
End Sub

I've had this problem (I'm using Microsoft 365), and I found this thread.我遇到了这个问题(我使用的是 Microsoft 365),我发现了这个线程。

It happens for me sometimes when I have another workbook already open, then, on trying to open my macro-enabled workbook, before any sheet is displayed I get the Microsoft warning message about macros.当我打开另一个工作簿时,有时会发生这种情况,然后在尝试打开启用宏的工作簿时,在显示任何工作表之前,我收到有关宏的 Microsoft 警告消息。 Then, although I click its 'Enable' button, the Workbook opens, macros do get enabled, but Workbook_Open doesn't run.然后,尽管我单击了它的“启用”按钮,但工作簿会打开,宏确实被启用,但 Workbook_Open 不会运行。

I've never known the problem to occur if no other workbook is open.如果没有打开其他工作簿,我从来不知道会出现问题。 (Of course, the user might still get the yellow-backed messages at the top of the workbook, asking them to click the Enable Editing and/or Enable Macros buttons.) (当然,用户可能仍会在工作簿顶部看到黄色背景的消息,要求他们单击“启用编辑”和/或“启用宏”按钮。)

Note that my Workbook_Open just calls another 'workbook-opening' sub in a module to do all the opening processing.请注意,我的 Workbook_Open 只是在模块中调用另一个“工作簿打开”子来完成所有打开处理。

My solution: When my workbook-opening sub is called, it sets a global variable to True to indicate it has run.我的解决方案:当我的工作簿打开子被调用时,它将全局变量设置为 True 以指示它已运行。

I've made it obvious to the user that the problem has occurred, by means of a 'Welcome' sheet with all its cells locked, so the user can do nothing;我已经通过“欢迎”表向用户表明问题已经发生,所有单元格都被锁定,因此用户无能为力; at this point all other sheets are very hidden.此时所有其他工作表都非常隐藏。 The workbook-opening sub, when it runs, deactivates this sheet and makes it very hidden, so the user never normally sees it, and makes the other sheets visible.工作簿打开子在运行时会停用此工作表并使其非常隐藏,因此用户通常不会看到它,并使其他工作表可见。 But if this screen remains, it instructs the user to select the other workbook, then select this one again.但是如果这个屏幕仍然存在,它会指示用户选择另一个工作簿,然后再次选择这个。 My Workbook_Activate code then runs, and because the global variable isn't True, it calls the workbook-opening sub.我的 Workbook_Activate 代码然后运行,并且由于全局变量不是 True,它调用工作簿打开子。 If this global variable is True, it does nothing.如果这个全局变量是 True,它什么都不做。

To make this work, the Workbook_Close sub makes the other sheets very hidden and the Welcome sheet visible, ready for the next time the Workbook is opened.为了完成这项工作,Workbook_Close 子使其他工作表非常隐藏,欢迎工作表可见,为下次打开工作簿做好准备。

Hey presto, the problem is solved.嘿嘿,问题解决了。

The Welcome sheet actually has a dual purpose, in that if either of the yellow-backed warning messages are displayed, it will remain and force the user, with suitable instructions, to click Enable Editing and/or Enable macros.欢迎表实际上有双重用途,如果显示任何一个黄色背景警告消息,它将保留并强制用户根据适当的说明单击启用编辑和/或启用宏。 If the users aren't au fait with macro-enabled Excel, they will just ignore these and try to carry on regardless.如果用户不熟悉启用宏的 Excel,他们将忽略这些并尝试继续。

All this is much easier to implement than to explain.所有这些都比解释更容易实现。 I hope it's clear.我希望很清楚。

And I hope this might be of help to someone.我希望这可能对某人有所帮助。

Excel 2016 (or 365) does not seem to fire the Workbook_Open() sub reliably or more precisely, not at all! Excel 2016(或365)似乎无法可靠地或更精确地触发Workbook_Open()子级,根本不会! The simple event sub简单事件子

Private Sub Workbook_Open()
    MsgBox "Work book is open"
End Sub

does not seem to work.似乎不起作用。 However, if a workbook is already open and then the workbook containing the above Sub is then opened, it does run as expected.但是,如果一个工作簿已经打开,然后打开了包含上述Sub的工作簿,它确实会按预期运行。 I notice that unlike Excel 2010, 2016 (365) opens each workbook in its own window, not a workbook window in the Excel application window.我注意到,与Excel 2010不同,2016(365)在其自己的窗口中打开每个工作簿,而不是在Excel应用程序窗口中打开工作簿窗口。 Is this a bug in 2016 and is there a workaround?这是2016年的错误,有解决方法吗?

I have produced a work around for my own applications and that is call the activation of a worksheet and call my initialization routines from there.我已经为自己的应用程序进行了变通,即调用工作表并从那里调用初始化例程。 But a bit "rough" and it would be good to have the Workbook_Open() sub working correctly.但是有点“粗糙”,让Workbook_Open()子正确工作会很好。

It is a simple single Sub in the ThisWorkbook module.它是ThisWorkbook模块中的一个简单Sub。 Macros are enabled.启用宏。 In Excel 2010 it works perfectly, as do two other macros in other workbooks for which I have written macros.在Excel 2010中,它可以完美工作,就像我为其编写宏的其他工作簿中的其他两个宏一样。 It is just this one thing with Excel 2016. Given that the Workbook_Open() sub is the gateway to a workbook it seems a little strange that we have to go to a workaround to make it function. Excel 2016就是一件事。鉴于Workbook_Open()子是工作簿的网关,我们不得不采取一种变通方法才能使其工作似乎有些奇怪。

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

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