Solved

Close all open Excel workbooks?

Posted on 2008-06-10
10
6,160 Views
Last Modified: 2012-08-14
Hello all,
I am trying to figure out a way to close all open excel workbooks with VB. The code I have selects the first instance of Excel and closes all workbooks within that, but if there is more than one instance of Excel then they do not close as well. Either closing the workbook or quitting the application is fine.

Stopping the excel.exe process does stop Excel, but it does not stop it gently so I am not looking for that.

Thanks a bunch.
set objOffice = GetObject(,"Excel.Application")
For Each WBook In objOffice.Workbooks
    WBook.Saved = True
    WBook.Close
Next

Open in new window

0
Comment
Question by:zabu99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 6

Expert Comment

by:Iced-evil
ID: 21753013
How about this?
  Dim objOffice As Excel.Application
  Set objOffice = GetObject(, "Excel.Application")
  do 
    objOffice.Quit
    Set objOffice = GetObject(, "Excel.Application")
  loop until err
  set objOffice = nothing

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21755457
Or, I haven't tested this, but what about.....
set objOffice = GetObject(,"Excel.Application")
For Each WBook In objOffice.Windows
    WBook.Saved = True
    WBook.Close
Next

Open in new window

0
 
LVL 5

Author Comment

by:zabu99
ID: 21755933
Thanks for the try mates, but neither works.
The first script goes for an infinite loop without closing Excel and the second does not seem to have any effect.

Revisions or new ideas welcome.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 65

Expert Comment

by:RobSampson
ID: 21755953
Try this.

Regards,

Rob.
Set objOffice = GetObject(,"Excel.Application")
objOffice.DisplayAlerts = False
For Each objWindow In objOffice.Windows
    objWindow.Activate
    Set WBook = objOffice.ActiveWorkbook
    WBook.Save
    WBook.Close
Next
objOffice.DisplayAlerts = True
objOffice.Quit

Open in new window

0
 
LVL 5

Author Comment

by:zabu99
ID: 21761053
Hi Rob,
It did not work. I placed a wscript.echo statement in the middle of the FOR loop to see if it ever activiated and it did not.

Thanks.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21764180
That's odd....it worked with my Excel 2003.....which version are you using?

As you open new workbooks, does it open new windows, or open them all in all in one?

Rob.
0
 
LVL 5

Author Comment

by:zabu99
ID: 21771759
I am running Win XP and Office 2003.
In order to be thorough, I used workbooks in the same window and workbooks in different windows.

I am very curious as to why it worked for you and not me.

Thanks for your efforts so far.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 21773905
OK, so I just realised that I was testing with *one* instance of Excel, but mutiple workbooks opened under that same instance.....

I then tested by running "Excel" separately a few times, and only one closed!

So, let's try something new, like looping through the Excel instances *and* windows, until it errors...

Regards,

Rob.
On Error Resume Next
While Err.Number = 0
	Set objOffice = GetObject(,"Excel.Application")
	objOffice.DisplayAlerts = False
	For Each objWindow In objOffice.Windows
	    objWindow.Activate
	    Set WBook = objOffice.ActiveWorkbook
	    WBook.Saved = True
	    WBook.Close
	Next
	objOffice.DisplayAlerts = True
	objOffice.Quit
	Set objOffice = Nothing
	WScript.Sleep 2000
Wend
MsgBox "Done"

Open in new window

0
 
LVL 5

Author Closing Comment

by:zabu99
ID: 31465830
No wonder you have 10 billion points. It works perfectly,
thanks.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21809959
LOL!  No problem.  Thanks for the grade.

Regards,

Rob.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question