Solved

Close all open Excel workbooks?

Posted on 2008-06-10
10
5,955 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

896 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

18 Experts available now in Live!

Get 1:1 Help Now