Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Closes all the excel files and exits excel application

Posted on 2004-08-08
6
Medium Priority
?
167 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 53

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

Industry Leaders: 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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses

604 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