Acces/Excel Automation Best Practices referrals
Posted on 2007-04-06
I don't have an immediate issue per se, so I'm planning on splitting points between the best three or four comments.
Main question: Is there a definitive book/reference/website for handling complex MS Office automation subjects? My specific area is automating Excel from Access.
I’ve developed an Access app that, in one sub for example, creates an xls, populates data and runs (Analysis Toolpak) regressions in a series of sheets, then saves it. Worked like a charm on a one-up basis. However, when the sub is called multiple times (40-50) as code loops through a recordset of selected states, it would hang intermittently.
After banging my head against the wall the requisite number of times (392), I ascertained the problem – the sub as written created an Excel application instance, toggled the Toolpak off and on, ran through the rest of the tasks, and then closed everything up, quit the app and set the references to nothing.
Unfortunately, that doesn’t seem to happen quickly enough when the next loop/request is coming in at the speed of light.
As a result, the Toolpak apparently didn’t always get started, so the code would error at unpredictable intervals (about 7 or 8 out of 45 files) when it hit the first regression call in a workbook. If the user clicked “OK” to the resulting error message, the code would start another instance of Excel in the next loop and go merrily on its way, leaving an orphaned instance of Excel running.
The fix was to start the Excel app instance and toggle the Toolpak addin in the calling routine (once!), then pass the app as a (excel.application) parameter to the sub with each loop, then kill it when the main routine completes (again, once).
So the question is – how should I have known that? I couldn’t find anything definitive on experts-exchange (although I got a hint from a thread about a sub that worked from debug but not when run from a form that eventually got me on track). Is there a Best Practices or help screen I’m not aware of that I should’ve stumbled onto sooner? Is it always a good idea to try to minimize the number of automation objects you instantiate and my mother just never told me? Is there a really good book or reference material or website that goes deep into this?
Thanks in advance for your thoughts and suggestions.