简体   繁体   English

Excel在Workbook_Open上崩溃

[英]Excel crashes on Workbook_Open

I have a complex workbook with quite some code, and used by many users spread over different places... and I am not a professional programmer. 我有一个复杂的工作簿,其中包含许多代码,并且被分布在不同地方的许多用户使用...而且我不是专业的程序员。 On the Workbook_Open event, I deal with a splash screen, password protecting and showing the main sheets and setting some global variables. 在Workbook_Open事件中,我处理了一个初始屏幕,使用密码保护并显示了主要工作表并设置了一些全局变量。 This file has been working reasonably stably for some months, but more recently I began working with the Ribbon to improve the user-interface (the goal is to replace a floating form that serves as a menu). 该文件已经可以合理稳定地运行了几个月,但是最近我开始使用功能区来改进用户界面(目标是替换用作菜单的浮动形式)。 It then became unstable, often crashing Excel on opening the file. 然后它变得不稳定,打开文件时经常使Excel崩溃。 Overall, the situation is very similar to this: Excel [VBA] crashes on Workbook Open when activating a sheet . 总体而言,情况与此非常相似: 激活工作表时,Excel [VBA]在“打开工作簿”时崩溃 I had already removed (or commented) all code related to Ribbon with no success, and the Wait command suggested on the thread above didn't help either. 我已经删除(或注释)了所有与Ribbon相关的代码,均未成功,并且上面线程中建议的Wait命令也无济于事。

If the workbook is opened in protected mode and then the code is enabled, it runs smoothly. 如果在保护模式下打开该工作簿,然后启用该代码,则它将顺利运行。 More interesting, if I close it without saving, it will run well next time I open it, without the need of the protected mode. 更有趣的是,如果我关闭它而不保存它,那么下次打开它时它将运行良好,而无需使用保护模式。 But as soon as I save the file, it will crash next time it is opened. 但是,一旦我保存文件,它将在下次打开时崩溃。

In fact, the crash is not immediate; 实际上,崩溃不是立即发生的。 it just seems that the code is in an infinite loop. 似乎代码处于无限循环中。 If I go to the VBAProject window, the title bar shows "[running...]" (or something alike - my installation is in French...), and if the ThisWorkbook module was opened before, we cannot see the code, but rather some frozen image of some part of the screen. 如果我转到VBAProject窗口,标题栏显示“ [running ...]”(或类似名称-我的安装使用法语...),并且如果ThisWorkbook模块之前已打开,则看不到代码,而是屏幕某些部分的冻结图像。 When I try to interrupt execution, then it really crashes. 当我尝试中断执行时,它确实崩溃了。

I already exported, deleted and re-imported all code and forms - no luck. 我已经导出,删除并重新导入了所有代码和表格-没运气。 The code is usually password protected, but I removed it for testing. 该代码通常受密码保护,但是我将其删除以进行测试。 Now I have no clue what to do. 现在我不知道该怎么办。 Any ideas, please? 有什么想法吗?

The code of the Open event is: Open事件的代码是:

Dim S As Object, rng As Range

'On Error GoTo Terminate

Application.Wait Now + #12:00:05 AM#

Application.ScreenUpdating = False
Application.EnableEvents = False
NSheets = Me.Sheets.Count

For Each S In Me.Sheets
    Set rng = AuxTables.Range("AT_SheetNames").Find(S.CodeName, , xlValues, xlWhole)
    If Not rng Is Nothing Then
        'Set sheet visibility
        If S.CodeName <> "Warning" Then S.Visible = Val(rng.Offset(0, 16))
        'Ensure all standard tabs are protected
        S.Protect Password:="xxxx", UserInterfaceOnly:=True, DrawingObjects:=False, _
            Contents:=True, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
            AllowFormattingRows:=True
        S.EnableOutlining = True
        'Erase marker of Degraded Mode
        S.Range(IIf(S.CodeName <> "CashCurves", "C", "B") & "1").ClearContents
        Call SetSheetsDefaults(S)
    End If
Next S

Warning.Visible = xlHidden

Call EnableRecalculate(True)
bl_UpdateSupplySummary = True
Application.EnableEvents = True
Application.ScreenUpdating = True
On Error Resume Next
    AppActivate Application.Caption, True
    Cover.Activate

Exit Sub

Terminate:
    Dim Msg As String
    Msg = "Description: " & Err.Description & Chr(10)
    Msg = Msg & "Module: Workbook" & Chr(10)
    Msg = Msg & "Procedure: Open" & Chr(10) & Chr(10)
    Msg = Msg & "Execution will be interrupted." & Chr(10) & Chr(10)
    Msg = Msg & "Please print this screen and send it to xxx@company.com"
    MsgBox Msg, vbCritical, "Unexpected Error " & Err.Number
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub

Thanks. 谢谢。

I don't know if this answer is applicable to the situation, but I stumbled upon these Q&A links (while looking into a somewhat-similar issue of my own), and thought they might be helpful (because editing the Ribbon sometimes involves using ActiveX) . 我不知道此答案是否适用于这种情况,但是我偶然发现了这些问答链接(同时研究了我自己的一个类似问题),并认为它们可能会有所帮助(因为编辑功能区有时涉及使用ActiveX )

ragoran; ragoran; 2010-07-22 on ExpertsExchange says he was encountering error 57121 during Workbook_Open code ( wsPrimary is the codename of a pre-exiting sheet), 2010年7月22日ExpertsExchange显示他在Workbook_Open代码( wsPrimary是预先存在的工作表的代号)期间遇到错误57121

The error say that wsPrimary does not exits (duh!... but it is there!!) If I Stop the macro execution, the spreadsheet continues to open then every thing is back to normal. 该错误表明wsPrimary不会退出(!! ...但是它在那里!)。如果我停止执行宏,电子表格将继续打开,那么一切恢复正常。

Seems like the macros is running before the spreadsheet has finish initializing all its object list... 似乎宏在电子表格完成其所有对象列表的初始化之前正在运行...

and the solution he found was: 他找到的解决方案是:

In a nut shell, if you have activeX control on some spreadsheet, and userform that have control bound to worksheet cells, and you compile your VBA while the form is opened in design mode, then you may cause some inconsitencies in the file which will create the behavior I have. 简而言之,如果您在某些电子表格上具有activeX控件,并且用户控件具有绑定到工作表单元格的用户窗体,并且在设计模式下打开窗体时编译了VBA,则可能会导致文件中出现一些不一致之处,从而导致我的行为。 .... this is my situation. ....这是我的情况。

The solution is to bind the control at run time and not design time. 解决方案是在运行时而不是设计时绑定控件。


Also, an answer to ragoran's question has this link (ExpertsExchange) where the problem is that 另外,对ragoran问题的答案具有此链接(ExpertsExchange) ,其中的问题是

when I try to save in Compatablity mode I get "Uninitialized ActiveX controls cannot be transferred to the selected file format. The controls will be lost if you continue." 当我尝试以兼容模式保存时,出现“未初始化的ActiveX控件无法传输为所选文件格式。如果继续,控件将丢失。”

Rory Archibald (2007-04-16) replies with 罗里·阿奇博尔德(2007-04-16)回复

"If a workbook contains ActiveX controls that are considered to be Unsafe for Initialization (UFI), they are lost when you save the workbook to an earlier Excel file format. You may want to mark those controls as Safe for Initialization (SFI). If you open a workbook with uninitialized ActiveX controls that are set to high security, you must first use the Message Bar to enable them before they can be initialized." “如果工作簿包含被认为对初始化不安全(UFI)的ActiveX控件,则在将工作簿保存为较早的Excel文件格式时,它们将丢失。您可能希望将这些控件标记为“初始化安全”(SFI)。如果打开具有未设置为高安全性的未初始化ActiveX控件的工作簿时,必须首先使用消息栏将其启用,然后才能对其进行初始化。”


I do realize that the symptoms they describe don't seem to match the OP's; 我确实意识到他们描述的症状似乎与OP的不匹配; however, these issues were happening in Excel 2003/2007, and -- if they are still issues -- may be manifesting differently in Excel 2010 or later. 但是,这些问题在Excel 2003/2007中发生,并且-如果仍然存在,则可能在Excel 2010或更高版本中有所不同。

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

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