简体   繁体   中英

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

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:

  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..
  2. Pressing a combination of keys: ex a macro has a Ctrl+F8 key associated with it.

  3. Click on a shape (mostly excel): check if this shape has a macro attached to it.

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".

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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