appExcel.Quit to quit the app then
set appExcel = nothing
Main Topics
Browse All TopicsI have the following code that creates a Workbook. After adding sheets and data the VBA exits.
How can I cleanly exit my sub without invoking error 462. I also get this error should the user choose to close the Workbook and the VBA is run again, in this instance Excel is left running in Tasks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Still left with Excel as a running process when the created workbook is closed. If it is left as such I can use the code at Workbook_Update to manage the creation of the Workbook. GetObject may error 429 but execution is directed back to Excel_Stopped to use CreateObject.
In Cleanup I am using brettdj idea of saving the workbook, closing it, then opening it using ShellExecute and apiFindWindow. Works OK but Excel process left running from VBA code no errors received.
1) As the code is running in a Public Sub, the running Excel process is closed when Access is closed.
2) If the Excel instance is stopped through task manager the code still creates an Excel instance but then an error 462 or 1004 occurs when trying to add worksheets. Exit is via Error_Cleanup
Hi cm,
Thanks for the help.
Curiously I had to add a couple of string variables to my Cleanup exit point. Normally they would lose scope when the Public Sub completed, however the calling Access form held the Public Sub routine in scope until the calling form was closed. I have not come across this behaviour before.
strWorkbook = vbEmpty
strFilePath = vbEmpty
My Cleanup exit point works as required now and the Excel process is completed, but success was achieved only after correcting the way some ranges were specified.
I had to explicitly state the Sheet in the line 'Set rngStart22 = sht2.Range .....' and all similar lines of code.
The previous method of 'Set rngStart22 = Range(rng,.....' gave the error 462 "The remote server etc" as reported by intellisense.
Business Accounts
Answer for Membership
by: brettdjPosted on 2009-10-26 at 17:02:09ID: 25668217
You could re-open the file in the host instance that contains the code above
Cheers
Dave