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.Visib le = False 'Excel is not shown
objExcel.windows(1).Visibl e = 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
To be specific, I have the following code that opens the excel file at MySource
Set objExcel = GetObject(MySource)
objExcel.Application.Visib
objExcel.windows(1).Visibl
......
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
Try this:
Dim oWkBk As Object
On Error GoTo Close_Error
For Each oWkBk In objExcel.Application.Workb ooks
oWkBk.Save
oWkBk.Close
Next
objExcel.Quit
Hope this helps.
Dim oWkBk As Object
On Error GoTo Close_Error
For Each oWkBk In objExcel.Application.Workb
oWkBk.Save
oWkBk.Close
Next
objExcel.Quit
Hope this helps.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
objExcel.Application.Quit
?