Solved

Closes all the excel files and exits excel application

Posted on 2004-08-08
6
164 Views
Last Modified: 2010-05-02
what is the code that closes all the "already open" excel files and exits the excel application in vb?

To be specific, I have the following code that opens the excel file at MySource

        Set objExcel = GetObject(MySource)
        objExcel.Application.Visible = False    'Excel is not shown
        objExcel.windows(1).Visible = True
......
        objExcel.Save
        objExcel.Close


In case MySource is already opened by the users. My code cannot be run. Since it is said that Excel cannot open the same file twice at the same time. I want to close all the "already opened" excel file and also close the Excel application so that my code can be run smoothly.


Any idea? Thanks

0
Comment
Question by:leekf
[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
6 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 11745840
Try like:

objExcel.Application.Quit

?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11745851
Try this:

          Dim oWkBk As Object

          On Error GoTo Close_Error

          For Each oWkBk In objExcel.Application.Workbooks
              oWkBk.Save
              oWkBk.Close
          Next

          objExcel.Quit

Hope this helps.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 60 total points
ID: 11745867
Private Sub Command2_Click()
On Error GoTo Command2_ClickError
Do
Set objexcel = GetObject(, "Excel.Application") 'Will set error if no instances open

        objexcel.Application.Visible = True   'Excel is not shown
        objexcel.windows(1).Visible = True
        objexcel.AlertBeforeOverwriting = False
        objexcel.Save
        objexcel.quit
Loop While True
Command2_ClickError:

End Sub

0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 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