VBA脚本关闭除自身以外的所有Excel实例 [英] VBA script to close every instance of Excel except itself
问题描述
我的错误处理函数中有一个子例程,该例程试图关闭在每个Excel实例中打开的每个工作簿.否则,它可能会保留在内存中并破坏我的下一个vbscript.它还应该关闭每个工作簿,而不保存任何更改.
I have a subroutine in my errorhandling function that attempts to close every workbook open in every instance of Excel. Otherwise, it might stay in memory and break my next vbscript. It should also close every workbook without saving any changes.
Sub CloseAllExcel()
On Error Resume Next
Dim ObjXL As Excel.Application
Set ObjXL = GetObject(, "Excel.Application")
If Not (ObjXL Is Nothing) Then
Debug.Print "Closing XL"
ObjXL.Application.DisplayAlerts = False
ObjXL.Workbooks.Close
ObjXL.Quit
Set ObjXL = Nothing
Else
Debug.Print "XL not open"
End If
End Sub
但是,此代码不是最佳的.例如,它可以在一个Excel实例中关闭2个工作簿,但是如果您打开2个excel实例,则只能关闭1个.
This code isn't optimal, however. For example, it can close 2 workbooks in one instance of Excel, but if you open 2 instances of excel, it will only close out 1.
如何重写此代码以关闭所有 Excel而不保存任何更改?
How can I rewrite this to close all Excel without saving any changes?
如何在不关闭承载此脚本的Access文件的情况下对Access进行此操作?
How to do this for Access as well without closing the Access file that is hosting this script?
推荐答案
我刚刚使用Excel和Access尝试了以下方法:
I just tried the following with both Excel and Access :
Dim sKill As String
sKill = "TASKKILL /F /IM msaccess.exe"
Shell sKill, vbHide
如果将msaccess.exe更改为excel.exe,excel将被杀死.
If you change the msaccess.exe to excel.exe, excel will be killed.
如果您想进一步控制该过程,请查看:
If you want a bit more control over the process, check out:
http://www.vbaexpress.com/kb/getarticle.php ?kb_id = 811
这篇关于VBA脚本关闭除自身以外的所有Excel实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!