Link to home
Start Free TrialLog in
Avatar of leekf
leekf

asked on

Closes all the excel files and exits excel application

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

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Try like:

objExcel.Application.Quit

?
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.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial