Excel Crashes When Opening Trusted Document With Macros Or Enabling Macros

This is caused by a macro with a compile error. To fix it, use the following steps (all credit to alulla at Ozgrid, whose directions I've lightly adapted for clarity):

  1. Open a new Excel workbook, not your crashing workbook
  2. Go to the Developer Tab and click “Macro Security” in the Code section of the Ribbon
  3. Click the bullet option entitled “Disable all macros with notification”
  4. Go to the Trusted Locations tab on the left and check the “Disable all Trusted Locations” box at the bottom
  5. Go to Trusted Documents and check the “Disable all Trusted Documents” box
  6. Click OK and close the new workbook
  7. Open your Excel file that was crashing
  8. Do not click "Enable Macros" - instead go to the Developer Tab and open the Visual Basic Editor
  9. In the VB Editor, save the project and then click Debug > Compile VBAProject
  10. Correct any compile errors that occur
  11. Save the VB project again and then save the Excel workbook
  12. Revert any changes you made to Macro Security in steps 3-5
  13. Close Excel, re-open the previously crashing workbook, and Enable Macros

I experienced today that this can also resolve "Can't find project or library" crashes where no reference is actually missing and Excel crashes before you can even get to the reference dialog in the first place.


Some Background/Findings

(see solution below if you don't care about this :) )
For us it happened in code that compiles and runs successful in Excel 2010, 2016 and 2019. So it must be some pretty bad compiled-code processing bug.
Our problem occurs at definite, always reproducible code locations. When one debugs/inspects the variables, strange things happen (other objects shown than expected, visual sections of excel elements get completely white/broken) ... as if it slowly internally crashes before it will disappear automatically (also from task manager) (sometimes without the immediate auto-restart dialog).

Solution (save with uncompiled code)

Our final 6 to 8 step solution is quite simple since it seems to be a bug with the runtime on compiled macro code workbooks that have been saved and opened anew:

  1. rename the workbook file, e.g. foo.xlsm to foo2.xlsm (untrusted name/file)
  2. open foo2.xlsm and do not enable macros
  3. open VBE editor (ALT+F11)
  4. (once/first time per file, e.g. foo2.xlsm):
    insert some CrashFix module
    • (right click project => insert module)
  5. write or update some only-existing comment line inside, e.g.

    'excel crash fix from https://stackoverflow.com/a/59876944/1915920: 2020-01-23 10:00
    
    • if already existing: just change anything there, e.g. change to current time
    • (=> the code, although only changed on comment-level, will be internally marked as uncompiled and will be recompiled on next open with enabled macros)
  6. close and save foo2.xlsm
  7. rename back foo2.xlsm to foo.xlsm
  8. optional: write protect file on file-level
    • may be only useful if updates are infrequent
    • prevents it from beeing saved accidentially with compiled code causing the crash on next open again

Now you should be able to open it without crashing (since obviously the initial compile run on startup prevents the crash somehow).

For others using the file (e.g. in a network share environment) it should now open without any macro-enable dialog. (for your own env it may ask again, since Excel "knows" somehow it was renamed inbetween on your system)

Tags:

Excel

Vba