Solved

Close all open Excel workbooks?

Posted on 2008-06-10
10
6,107 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

My experience with Windows 10 over a one year period and suggestions for smooth operation
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

713 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