Solved

Closes all the excel files and exits excel application

Posted on 2004-08-08
6
165 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 52

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

688 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