1

Could someone please let me know if the following simple VBScript is correct? It is supposed to close Excel after other processes have run (and left Excel open), but it doesn't work.

Set MyApp = CreateObject("Excel.Application")
MyApp.Quit
  • Doesn't work? Do you get an error or does it not run as expected? – Nick.Mc Jun 22 '15 at 01:11
  • Just doesn't do anything.... no error shown. – Cristina Fiacconi Jun 22 '15 at 01:16
  • 1
    So you're double clicking the `.VBS` file? For troubleshooting purposes I usually put this code around the outside: `MsgBox "Start"` then `MsgBox "Complete"` which at least tells me the script ran. If you also add `MsgBox Myapp.Name` after you set `MyApp` it will also tell you something. But the real problem is that you are using `CreateObject` which simply instantiates a new excel. It doesn't get existing ones. – Nick.Mc Jun 22 '15 at 02:31
  • Add the bottom of this page you will find an example of some code that iterates through each process and shows it. You should be able to alter the script to find only Excel process and add some more code to grab and close them (or kill them). But you should really find the bad code that is leaving orphan excel processes and fix it – Nick.Mc Jun 22 '15 at 02:38
  • Thanks for your replies. I have a batch file that runs the VBScript, but it does not close Excel after closing the file. – Cristina Fiacconi Jun 22 '15 at 08:38
  • Sorry I forgot to post the link in my prior comment but it seems you have a solution. I strongly suggest you fix your batch file instead of cleaning up orphaned Excel processes afterwards. – Nick.Mc Jun 22 '15 at 09:20

3 Answers3

4

CreateObject creates a new object. If I understand your question correctly you want to attach to already running (orphaned) Excel processes to terminate them. You can do that with GetObject:

On Error Resume Next
Do
  Set xl = GetObject(, "Excel.Application")
  status = Err.Number
  If status = 0 Then
    For Each wb in xl.Workbooks
      wb.Close False  'discard changes in open workbooks
    Next
    xl.Quit
  ElseIf status <> 429 Then
    WScript.Echo Err.Number & ": " & Err.Description
    WScript.Quit 1
  End If
Until status = 429
On Error Goto 0

Note that this will try to close all running Excel instances, discarding all changes in open workbooks. If you want it to save changes in open workbooks change the argument of the Close method to True. If you have Excel instances you want to keep running, you need to add code to exclude them from being closed.

Note also, that this will not forcibly terminate unresponsive instances. You'd need to kill the process for that:

Set wmi = GetObject("winmgmts://root/cimv2")
For Each xl In wmi.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'excel.exe'")
  xl.Terminate
Next
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

Try this please.

ThisWorkbook.Saved = True
Application.Quit
Baz Guvenkaya
  • 1,482
  • 3
  • 17
  • 26
0

CreateObject creates a COM object, so your

Set MyApp = CreateObject("Excel.Application") 

starts a new Excel process. Use GetObject to "retrieve an existing object with the specified ProgID". See this for theory and praxis.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96