I’m a relative VBA novice, and designing a fairly complex macro that can run for anything between 10 seconds and 10 minutes, which does most of its work in another invisible instance of Excel created by Dim appDatabaseInstance = New Excel.Application . There are a number of functions and subroutines that get used over the course of code execution, and whilst I’ve done my best to ensure that appDatabaseInstance.Quit is called whenever anything semi-predictable goes wrong, there’s one thing I can’t control – the user.
Specifically, if a user decides the program has crashed and holds escape, VBA will break execution, and because of how the break took place, none of my garbage collection routines (I think) end up taking place.
As such, I’d be much happier if I could somehow name the instance I create in a predictable manner, and then attempt, before creating a new instance, to find an existing one and deal with it appropriately. That way even if an unfortunate application instance gets left open, it will get closed as soon as the user tries anything else involving the programme (and if they don’t, then one instance is unlikely to cause too many headaches in the context).
Does anyone have any suggestions?
You can use the
EnableCancelKeyproperty of the Application object to determine what will happen when a user interrupts your codeI suggest you see this post on EnableCancelKey at Dick’s Blog.
As a summary,
EnableCancelKeyhas three possible settings: