Solved

Closes all the excel files and exits excel application

Posted on 2004-08-08
6
161 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
6 Comments
 
LVL 49

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code 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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

757 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