How to close ALL instances of Excel

S

spIky haIred

Hi everyone,

I would like to close ALL instances of Excel... I am able to close all
workbooks within an instance of Excel but cannot close workbooks in
other instances of Excel... Is there a way to scan all open windows? Or
even better, to scan all open instances of Excel only?

Help would be much appreciated.
Thanks in advance,
Jason
 
N

NickHK

This code is to run in Excel VBA ?
You want to close your own instance ?

Depending on the above GetObject may not be the answer, as you cannot
stipulate which instance gets returned.
Maybe use the API FindWindow with "XLMAIN" and SendMesssage with "WM_CLOSE"
?

NickHK
 
G

Guest

Application.Quit is one way, but you might get a few dialog boxes to respond
to if you don't either save the workbooks or mark them as saved before you
apply it.
 
S

spIky haIred

hi all, thanks for your help on this, i have tried application.quit but
it still leaves the other "instances" of excel open...
Nick i shall try your solutions, thanks again
 
S

spIky haIred

sorry guys, couldnt get the solutions to work...
any more advice?
let me rephrase my question... probably wasn't clear at the start..
im trying to close all excel workbooks in any number of windows using
either excel vba or vbscript...
thanks again
 
S

spIky haIred

sorry guys, couldnt get the solutions to work...
any more advice?
let me rephrase my question... probably wasn't clear at the start..
im trying to close all excel workbooks in any number of windows using
either excel vba or vbscript...
thanks again
 
S

spIky haIred

sorry guys, couldnt get the solutions to work...
any more advice?
let me rephrase my question... probably wasn't clear at the start..
im trying to close all excel workbooks in any number of windows using
either excel vba or vbscript...
thanks again
 
N

NickHK

So you are closing Workbooks in the same (your current) instance of Excel ?
Dim WB As Workbook
For each wb in workbooks
if wb.name<>thisworkbook.name then
wb.close 'True/false depending on your requirements
end if
next

VBScript would be the same, but with an added
Dim XLAPP as object
Dim WB As Workbook

set xlapp=getobject("Excel.Application")
if not xlapp is nothing then
For each wb in xlapp.workbooks
wb.close 'True/false depending on your requirements
next
end if

Clossing all WBs in all instances from VBScript
Dim XLAPP as object
Dim WB As Workbook

do
set xlapp=getobject("Excel.Application")
if xlapp is nothing then exit sub
For each wb in xlapp.workbooks
wb.close 'True/false depending on your requirements
next
loop

You should add error handling

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top