简体   繁体   English

从外部退出Excel宏(从GUI,而不是从宏)

[英]Quit an Excel Macro externally (from a GUI, not from the macro)

I'm have a program (GUI) that interfaces with excel to execute macros. 我有一个与Excel交互以执行宏的程序(GUI)。 We're using Microsoft.Office.Interop.Excel to call/run the macros and this works great. 我们正在使用Microsoft.Office.Interop.Excel来调用/运行宏,并且效果很好。

What I can't figure out is a good way to cancel the macros from the GUI. 我不知道是从GUI取消宏的好方法。

One idea we had was to use the excel.application variable that runs the macros to write a "stop" value to a specific cell in the workbook, and in the macros (they are all mostly loops), check for the "stop" value in that cell. 我们曾经想到的一个方法是使用运行宏的excel.application变量将“停止”值写入工作簿中的特定单元格,然后在宏(它们大多是循环的)中检查“停止”值在那个牢房里。

This crashes my GUI with this 这使我的GUI崩溃

The program '[2188] BVLReports.vshost.exe: Managed' has exited with code -336589910 (0xebf00baa).

And excel gets tied up, and won't respond. 而且excel被束缚了,不会响应。 I know how to Exit Sub from within the macro if the "stop" value exists, so I don't need answers on how to check/cancel from inside the macro. 我知道如果存在“ stop”值,如何从宏中Exit Sub ,因此我不需要有关如何从宏内部进行检查/取消的答案。 Any ideas on a better way to write this "stop" value or a better way to cancel the macros externally? 关于编写此“停止”值的更好方法或从外部取消宏的更好方法有什么想法?

Thanks 谢谢

This might be a case for the mysterious Excel Application.CheckAbort feature? 神秘的Excel Application.CheckAbort功能可能就是这种情况?

Application.CheckAbort Application.CheckAbort

According to Help Application.Checkabort is supposed to stop recalculation except for a specified range. 根据帮助应用程序,Checkabort应该停止重新计算,除了指定的范围。 It does not do this. 它不会这样做。 Apparently it throws a runtime error if there are pending abort messages in the app message queue (mouse clicks, esc key down etc). 显然,如果应用程序消息队列中有挂起的中止消息(鼠标单击,Esc键按下等),它将引发运行时错误。 The parameter specifies whether to eat the message or leave it in the queue. 该参数指定是吃消息还是将消息留在队列中。 The objective is to allow aa long-running VBA calculation to be interrupted in the same way as an Excel calculation. 目的是允许长时间运行的VBA计算以与Excel计算相同的方式中断。

Application.CheckAbort ([KeepAbort]) Application.CheckAbort([KeepAbort])

Maybe you can use the SendKeys function to send keyboard output to excel and kill it that way. 也许您可以使用SendKeys函数将键盘输出发送到excel并以此杀死它。

Hope this helps. 希望这可以帮助。

Enjoy! 请享用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM