Solved

Close all open Excel workbooks?

Posted on 2008-06-10
10
5,895 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now