Dev Luv: Determining When a Shape is Copied versus Added

A blog reader recently sent me mail asking how to programmatically determine if a shape has been added to the page through a cut-and-paste operation or by the user dragging and dropping the shape from the stencil. In this case, the reader wanted to call a different routine when the shape is copied. Visio has a ShapeAdded event but no ShapePasted event. In order to determine the context in which the shape was added, you’ll have to use the EnterScope event.

Visio creates a scope for every internal Visio command. To check out how scope works, open the Event Monitor (which ships in the Visio SDK) while running Visio. Cut and paste a shape. In the event scope, you’ll see the command ID for copy: EnterScope Paste [1022;0;Paste]

You can listen to the EnterScope event and parse for the copy ID (1022) or whatever command ID you care about. The ID is the equivalent of the command-related constants that begin with visCmd*, which are documented in Visio developer help. If you are handling this event using AddAdvise, the EnterScope event also records extra information in the EventInfo property of the Application object. In addition, the varMoreInfo argument to VisEventProc will contain a string formatted as follows: [<nScopeID>;<bErrOrCancelled>;<bstrDescription>]

One thing to keep in mind: There’s only one flavor of EnterScope event so only use it when you have to. EnterScope will queue up when each internal Visio command begins and each custom solution opens a scope (see the SDK’s Code Librarian for samples on how to do scope participation – look under the Events section). You should prototype performance for your application first before committing to a design where you need to route your logic according to EnterScope strings.

-- Mai-lan


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