简体   繁体   中英

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. 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. Overall, the situation is very similar to this: Excel [VBA] crashes on Workbook Open when activating a sheet . 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.

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. 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:

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) .

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),

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.

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. .... 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

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."

Rory Archibald (2007-04-16) replies with

"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."


I do realize that the symptoms they describe don't seem to match the OP's; however, these issues were happening in Excel 2003/2007, and -- if they are still issues -- may be manifesting differently in Excel 2010 or later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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