简体   繁体   English

MS Office 自动化:在宏运行时收到通知(事件)

[英]MS Office Automation: Get notified (Event) when a macro runs

I'm developing an MS Office (Excel, Word, PowerPoint and Outlook) Add-in using C#/.NET, which tracks and logs some events when occurred as DocumentOpen, DocumentContentChange..etc However, and after days of searching, I'm unable to find a way (an event) to get notified when a macro is run on an Excel Workbook, Word Document.. etc我正在使用 C#/.NET 开发一个 MS Office(Excel、Word、PowerPoint 和 Outlook)加载项,它会跟踪和记录一些发生在 DocumentOpen、DocumentContentChange 等事件时的事件。但是,经过几天的搜索,我当在 Excel 工作簿、Word 文档等上运行宏时,我无法找到一种方法(事件)来获得通知

The hard way is to try to catch all the possible paths the user can follow to run a macro, using Mouse & Keyboard Hooking Win API, which are:困难的方法是尝试捕获用户可以遵循的所有可能路径来运行宏,使用鼠标和键盘挂钩 Win API,它们是:

  1. Using the "Macros" Dialog (using purely Win API for accessing the window): OnClick, If this is the ActiveWindow, and "Run" button has the focus, read the value in the TextBox under "Macro name:" label, but I found this approach to be very complex since there are some cases like: the user press ENTER and the focus is not on the "Run" button or if the user DoubleClick on an item in the list of macros which run the macro..使用“宏”对话框(使用纯 Win API 访问窗口): OnClick,如果这是 ActiveWindow,并且“运行”按钮有焦点,请读取“宏名称:”下文本框中的值:label,但我发现这种方法非常复杂,因为在某些情况下,例如:用户按 ENTER 键并且焦点不在“运行”按钮上,或者如果用户双击运行宏的宏列表中的项目..
  2. Pressing a combination of keys: ex a macro has a Ctrl+F8 key associated with it.按组合键:例如,宏有一个与之关联的 Ctrl+F8 键。

  3. Click on a shape (mostly excel): check if this shape has a macro attached to it.单击一个形状(主要是 excel):检查该形状是否附加了宏。

I can see that these approaches are very limited.我可以看到这些方法非常有限。

Have you got better suggestions?你有更好的建议吗?

The way that I do it is store the data in a table in a database when the macro is run. 我这样做的方法是在运行宏时将数据存储在数据库的表中。 That way the code is in the macro and you can be sure that if the macro is run, the data will be in the database. 这样代码就在宏中,你可以确定如果宏运行,数据将在数据库中。

Please see the events logged under "application and service logs" (separate from "windows logs"), then "Microsoft Office alerts".请查看“应用程序和服务日志”(与“Windows 日志”分开)下记录的事件,然后查看“Microsoft Office 警报”。

There are different types of alerts, including these related to macros - perhaps you will find there the information you seek.有不同类型的警报,包括与宏相关的警报 - 也许您会在那里找到您要查找的信息。

Please let us know about the results.请让我们知道结果。 This is not very well documented event storage.这不是很好记录的事件存储。

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

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