lcha
asked on
vba auto close files after break in code
Hello experts,
I have a vba macro that opens 3 files but if I intervene and break/stop the code in middle of processing, the files appear to be still be in memory in the task manager.
I need a code sample of on how automatically close the open files if there's an error in the code or if I break the code in the middle of running a macro.
Thanks in advance for your help!
I have a vba macro that opens 3 files but if I intervene and break/stop the code in middle of processing, the files appear to be still be in memory in the task manager.
I need a code sample of on how automatically close the open files if there's an error in the code or if I break the code in the middle of running a macro.
Thanks in advance for your help!
ASKER
thanks! I added this code substituting the object names you provided with the ones in my code but it doesn't seem to be working properly.
Here's some additional info.
I start out by opening to excel files. One is an excel file and another that is a macro enabled excel file.
I run the code from the macro enabled file by clicking f8 and run the code in the macro enabled file.
As the macro runs, it opens 2 other files creates and opens 3rd output.xls file.
When I hit the "esc" key to break the code in the middle of processing, I get a popup that says the "code execution has been interrupted. Then I click the "end" button.
After that is all done, all my files are open still. Next, I manually close all the open excel files and check the task manager. There is still one excel process running.
Here's some additional info.
I start out by opening to excel files. One is an excel file and another that is a macro enabled excel file.
I run the code from the macro enabled file by clicking f8 and run the code in the macro enabled file.
As the macro runs, it opens 2 other files creates and opens 3rd output.xls file.
When I hit the "esc" key to break the code in the middle of processing, I get a popup that says the "code execution has been interrupted. Then I click the "end" button.
After that is all done, all my files are open still. Next, I manually close all the open excel files and check the task manager. There is still one excel process running.
Dim oeXcel as New Excel.Application (I'm not sure if declaration in my code is even needed.... it's not used anywhere else)
Err.Raise 12
' do stuff
wb.Close False
WB_input2.Close
WB_input3.Close
outputWB.Close
oExcel.Quit
Set oExcel = Nothing
Exit Sub
errhandler:
wb.Close False
WB_input2.Close
WB_input3.Close
outputWB.Close
oExcel.Quit
Set oExcel = Nothing
ASKER
Here's some additional info (See CAPs for my correction)
I start out by opening TWO excel files. One is an excel file and another that is a macro enabled excel file.
I run the code from the macro enabled file by clicking f8 and run the code in the macro enabled file.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
something like below, see the errhandler code that closes the automated workbook and excel object
Cheers
Dave
Open in new window