Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Close all open Excel workbooks?

Posted on 2008-06-10
10
Medium Priority
?
6,582 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

926 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