2

I am running Excel as a calculation engine (no user interaction) through VSTO and would like to be able to cancel a long running macro programmatically.

My current approach is to put the blocking call to run the Macro (Application.Run("MyMacro")) on a separate thread and then attempt to interrupt it from the main thread.

There does not seem to be any in built method call for this purpose such as Application.Interrupt(), so I am trying to programmatically trigger the keyboard interrupt that occurs when CTRL+Break or ESC are pressed.

Current code looks like:

Application.CalculationInterruptKey = XlCalculationInterruptKey.xlAnyKey;
Application.EnableCancelKey = XlEnableCancelKey.xlInterrupt;

Application.SendKeys("{ESC}", false); //have tried with true as second argument 

In this case the code blocks on the final line which does not execute until the macro has completed.

I have also tried obtaining the process hook for Excel, setting it as the active process / window and calling SendKeys without success.

As this needs to be a generic solution for executing any macro, I cannot modify or provide any guarantees as to the macro contents.

Any ideas?

Thanks,

PS I have also tried sending the CTRL+BREAK keyboard command as in Application.SendKeys("^{BREAK}", false); The debugger pauses on this line until the macro finishes execution, so it seems like the Application object is blocked from processing any more commands until the macro has completed.

davester
  • 21
  • 2

0 Answers0