When I began to think about the previous post, (on automating the insertion of images into an Org Chart), I started by running the Visio Event Monitor tool while manually inserting an image using the Add-On's right click option to see the results. Rather than squeeze all of the technical explanation into the same post, I thought I'd add a second one to go into all of the gory details and also introduce the Event Monitor tool for anyone that hasn't come across it before...
The Event Monitor is a tool that comes with the Visio SDK and the Help file describes it as follows:
"The Microsoft® Office® Visio® Event Monitor tool watches the events that fire in an instance of Visio and logs all events of the types that you specify in the Event Options dialog box (Tools menu). It can help you determine which events to handle in your own solutions."
I suppose in some senses it is somewhat akin to the the Macro recorder in that you can see the effects of actions that you take in the UI and this gives you a better understanding of what's going on under the covers.
Getting started
Once you've downloaded and installed the SDK you'll find, amongst other things, a new menu item for the tool under Tools / Add-Ons / SDK / Tools / Event Monitor... Click this, and you'll find the tool leap into life and spew out a furious stream of text. (Note - It's worth resizing both Application and tool windows so you can see both at once.)
Under most conditions there are a number of events you can stop monitoring so you can begin to see the wood for the trees. You can get to this by bringing up the Event Options dialog from the tool's Tools menu and what you'll see is something like this:
The main ones to remove first are MouseMove, NoEventPending, VisioIsIdle and MustFlushScopeBeginning/Ended. Enter/ExitScope are to do with Undo/Redo functionality and can be worth keeping in as they are often named and so aid in identify groups of changes. Of course your objective for using the tool will ultimately determine which events you want to observe, but in most cases getting rid of the above ones can help reduce the clutter.
A final point I'd like to highlight is the tool's control buttons. As you running Visio it can sometimes be useful to pause monitoring to study the results and then restart again when you're ready. You can do this with the Suspend and Resume buttons on the toolbar.
The Org Chart Example
Now the problem with the Org Chart add-on is that although it offers some level of programatic access (see Office Online for further details), adding multiple images isn't included.
To get a good idea of what's going on, we're interested mostly in the FormulaChanged event as this will tell us which shapes and cell formulae are being added or changed. (Note - the CellChanged event gets fired all over the place so it can be filtered out.)
Starting up the monitor and then adding an image to an existing Org Chart shape produces the following results (I've cut down the information to fit into this narrow column but you can see the full size version here):
This is great. We can see a shape's been added, named Sheet.12, and then a series of things happen to both the new shape and the Org Chart shape we're adding the image to (named Executive in this instance).
The Event Monitor's output differs depending on the events fired, but in the case of FormulaChanged it follows this format:
- an event sequence number (not shown above)
- the event name (in black above)
- the cell name that changed (in blue)
- the resulting cell value (in red)
- the new formula (in green)
- additional event information in square brackets (not shown)
So we now have pretty much all of the details we need to produce our own code. I say pretty much as there are a few things that aren't picked up. For example, we don't get to see any code or events that happen inside the add-on such as the calculation of the PictureAspectRatio (seen in the third line above) so we have to deduce what it's doing (not too hard in this case) and write our own code to produce the same results. The important thing here is that we get to see the ShapeSheet cells that are being written to and what is actually being added.
The end result
So without too much work you can create your own version of the image adding functionality and loop through all of the shapes calling the procedure on each one:
[Update 11th Feb 2019 - If you come across this post first, you might also be interested in a more recent related post here: Enhancing Visio Event Monitor Output]