“ThisWorkbook.Close” Causes excel to crash

孤街浪徒 提交于 2019-12-23 10:15:25

问题


I have a large collection of excel files that work almost like a program, and they run on multiple computers (with different versions of windows), and as of latelly I have this bad problem, when the user presses my close button(actually a picture I associate a macro with), with the code calls:

ThisWorkbook.Close savechanges:=True

It causes 2 of the 4 supported computers to crach EXCEL (Windows XP = OK, Windows 10 = OK 1 BAD OTHER, Windows 8 = BAD).

I have isolated the incident to this particular line of code (made a 1 sheet excel file with just a close button, and it still crashes) I have noted that if the excel file isn't the only one open, sometimes it doesn't crash (maybe the problem is with closing excel itself)

What I have done is separeted the 2 statements so if (when) it crashes it's already saved:

ThisWorkbook.Save
ThisWorkbook.Close

Can anyone shed some lights? I'm really lost. I tried all the alternatives I could think off (activeworkbook...)

Tl;dr: "ThisWorkbook.Close" Causes excel to crash


回答1:


This is a standard bug in Microsoft Excel. Not sure if Microsoft has any fix. However, there are workarounds to overcome this issue.

This issue occurs when "Close" event is triggered from a click event but works fine with other event like "Selection Change". To tackle this issue, you may try this one:

Add the following code in the click event of the button:

Private Sub CloseButton_Click()
    Cancel = True
    Application.OnTime Now, "Close_Xls"
End Sub

In a standard module, add the following code

Sub Close_Xls()
   ThisWorkbook.Close  savechanges:=True
End Sub

It works for me. Let me know if it is helpful



来源:https://stackoverflow.com/questions/40524060/thisworkbook-close-causes-excel-to-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!