vba Application.Quit 命令不会关闭整个 Excel 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18738677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Application.Quit command not closing the entire Excel Application
提问by logan
I have following code under a button. When clicked it just closes the current Excel sheet but not the entire Excel application.
我在按钮下有以下代码。单击时,它只会关闭当前的 Excel 工作表,而不是整个 Excel 应用程序。
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
Note:I don't have any other sheets open.
注意:我没有打开任何其他工作表。
The following window still appears.
仍会出现以下窗口。
回答by FranckH
I had this issue and I resolved it by putting in the Workbook_BeforeClose()
:
我遇到了这个问题,我通过放入以下内容解决了它Workbook_BeforeClose()
:
ThisWorkbook.saved = true
回答by SeanC
remove the Application.DisplayAlerts = True
from the routine.
Application.DisplayAlerts = True
从例程中删除。
from the help for Application.Quit Method
:
来自帮助Application.Quit Method
:
If unsaved workbooks are open when you use this method, Microsoft Excel displays a dialog box asking whether you want to save the changes. You can prevent this by saving all workbooks before using the Quitmethod or by setting the DisplayAlertsproperty to False. When this property is False, Microsoft Excel doesn't display the dialog box when you quit with unsaved workbooks; it quits without saving them.
如果使用此方法时未保存的工作簿处于打开状态,Microsoft Excel 将显示一个对话框,询问您是否要保存更改。您可以通过在使用Quit方法之前保存所有工作簿或将DisplayAlerts属性设置为False来防止这种情况发生。当此属性为False 时,当您使用未保存的工作簿退出时,Microsoft Excel 不会显示对话框;它退出而不保存它们。
This will avoid any (possibly hidden) prompts from stopping excel from closing completely
这将避免任何(可能隐藏的)提示阻止 excel 完全关闭
回答by Tomek B.
The window does not close because you are using personal.xlsb. Cut Personal.xlsb and paste in another location.
该窗口未关闭,因为您使用的是 Personal.xlsb。剪切 Personal.xlsb 并粘贴到另一个位置。
Instead of Personal.xlsb create and work on modules. It's a better option.
而不是 Personal.xlsb 创建和处理模块。这是一个更好的选择。
回答by Tristan Molinié
"ThisWorkbook.Saved = True" after "Application.Quit" works on Excel 2016
“Application.Quit”后的“ThisWorkbook.Saved = True”适用于 Excel 2016
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.Quit
ThisWorkbook.Saved = True
End Sub
回答by Nils
I did not try it, but maybe this will help:
我没有尝试过,但也许这会有所帮助:
According to Norie you might not have anymore workbooks open, therefore Application.Quit will never be executed.
根据 Norie 的说法,您可能不再有工作簿打开,因此 Application.Quit 将永远不会执行。
AlphaFrog therefore suggests this:
因此,AlphaFrog 建议:
Application.DisplayAlerts = False If Application.Workbooks.Count = 1 Then Application.Quit Else ActiveWorkbook.Close End If
Application.DisplayAlerts = False If Application.Workbooks.Count = 1 Then Application.Quit Else ActiveWorkbook.Close End If
回答by Joseph Shinaberry
I had the same issue using the following code closed excel cleanly:
我使用以下代码完全关闭excel时遇到了同样的问题:
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.Quit
This will allow excel to cleanly close without keeping a "ghost" window open.
这将允许 excel 干净地关闭,而不会打开“鬼”窗口。
回答by Joey
I experienced the same issue and was able to resolve the issue with code that looks to see if multiple workbooks are open or not ...
我遇到了同样的问题,并且能够通过查看多个工作簿是否打开的代码来解决该问题......
Application.EnableEvents = False
Application.DisplayAlerts = False
If Application.Workbooks.Count = 1 Then 'Close Excel application
ThisWorkbook.Save
Application.Quit
Else 'Close the active workbook
With ActiveWorkbook
.Close Savechanges:=True
End With
End If
回答by Cheng Tsai
This is a strange one, hopefully someone will find this answer useful. I ran into something very similar using Excel 2010 (14.0). I stumbled to my answer through experimentation. This is bad answer for general purpose.
这是一个奇怪的问题,希望有人会发现这个答案有用。我使用 Excel 2010 (14.0) 遇到了非常相似的问题。我通过实验偶然发现了我的答案。这是一般用途的错误答案。
For whatever reason Application.Quit fails silently if the option AccessVBOM is not enabled. It is not enabled out of the box and can be set/unset by your network admin by windows policy.
无论出于何种原因,如果未启用 AccessVBOM 选项,Application.Quit 都会以静默方式失败。它不是开箱即用的,可以由您的网络管理员通过 Windows 策略设置/取消设置。
You can find this option in the GUI by traversing "Excel Options" -> "Trust Center" -> "Trust Center Settings" -> "Macro Settings" -> "Trust access to the VBA project object model". Or programmatically.
您可以通过遍历“Excel选项”->“信任中心”->“信任中心设置”->“宏设置”->“信任访问VBA项目对象模型”在GUI中找到该选项。或以编程方式。
Since we all love code, in this example we are running Excel from C# interop and calling the quit function.
由于我们都喜欢代码,因此在本示例中,我们从 C# 互操作运行 Excel 并调用退出函数。
using Excel = Microsoft.Office.Interop.Excel;
using Marshal = System.Runtime.InteropServices.Marshal;
Excel.Application app = new Excel.Application();
app.Visible = false;
app.DisplayAlerts = false;
// this will hang if AccessVBOM is not enabled
app.Quit();
Marshal.ReleaseComObject(app);
回答by ram
Have passed MacroName from bat file and tried the below code its working. But one thing I observed is if we are closing the workbook(ActiveWorkbook.Close) before Application.Quit then it is not working.
已经从 bat 文件中传递了 MacroName 并尝试了以下代码的工作。但我观察到的一件事是,如果我们在 Application.Quit 之前关闭工作簿(ActiveWorkbook.Close),那么它就不起作用。
Private Sub Auto_Open()
Dim strMacroName As String
strMacroName =
VBA.CreateObject("WScript.Shell").Environment("process").Item("MacroName")
If strMacroName <> "" Then Run strMacroName
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
If strMacroName <> "" Then Application.Quit
End Sub
Sub Button1_Click()
MsgBox ("done")
End Sub
回答by Joel Mazza
Make sure that your sheets do not have any external link references, especially broken links.
确保您的工作表没有任何外部链接引用,尤其是损坏的链接。
I struggled with this problem for more than a week, rewriting and commenting out lots of code to try to isolate the problem. I finally did a review of all table and external sheet references in my workbook this morning. I removed all unnecessary links and broken references and the workbook now closes without hanging in memory.
我在这个问题上挣扎了一个多星期,重写和注释了大量代码以尝试隔离问题。今天早上,我终于对工作簿中的所有表格和外部工作表引用进行了。我删除了所有不必要的链接和损坏的引用,工作簿现在关闭而不挂在内存中。