“Calling” COM add-ins from the ShapeSheet


Many developers are used to calling VSL or EXE add-ons from ShapeSheet cells. Many of us, however, have fallen in love with C# and VB.NET. VSL's are primarily written using VC++. You can write EXE add-ons in managed code, but you want a solution that runs in-process. So what's a Visio developer gotta do? The answer is to create a COM add-in. The puzzling question that arises when attempting this approach is, "how do you 'call' a COM add-in from the ShapeSheet?"

As you know, COM add-ins are usually loaded on startup with Visio. Because they are already loaded and running, you don't really "call" COM add-ins as we do with VSL or EXE add-ons. Instead, from the ShapeSheet cell you would raise an event and handle it in your COM add-in. Visio provides the MarkerEvent event that allows you to encode application specific information in its ContextString parameter. By convention, the data that developers pass using the ContextString parameter look a lot like command line switches (e.g. "/solution=MyAddIn /cmd=100"). When the event is raised, your COM add-in would handle it by parsing the context string, determining if the marker event belongs to you, and by executing the specified command. The MarkerEvent event is the essential device to "call" commands in your COM add-in.

There are three ways of raising a marker event from a ShapeSheet cell: by using the Application.QueueMarkerEvent method, by running the QueueMarkerEvent add-on (using the RUNADDONWARGS ShapeSheet function), or by using the new QUEUEMARKEREVENT ShapeSheet function. The first way was used in a VBA procedure in conjunction with the CALLTHIS ShapeSheet function (yuck!). The second approach was introduced in Visio 2002 SR1 and is still highly useful when combined with the Persistent Events tool. And finally, Visio 2003 provides the QUEUEMARKEREVENT ShapeSheet function. Needless to say, you should raise marker events from the ShapeSheet using this function.

Here’s a recap:

  1. In your COM add-in, listen to QueueMarkerEvent events (using AddAdvise, of course).
  2. In an event cell (like EventDblClick), call the QUEUEMARKEREVENT function and specify the context string (e.g. "/solution=MyAddIn /cmd=100").
  3. In your MarkerEvent event handler, parse the context string.
    1. Check if you raised the marker event (by checking the solution argument). If you didn’t raise the marker event, stop there.
    2. If it is yours, then execute the appropriate command (specified by the cmd argument).

 

-Chris

 

 

This posting is provided "AS IS" with no warranties, and confers no rights