VXDSP Support Notes: 
Grouping Interacts with Shape and Cell References and Events
Home Visio VXDSP VXDSP Support Notes This page

Overview

This page discusses a not-so-obvious behavior of Visio relating to what happens to Shape and Cell references (and their events) when a user groups or ungroups the shape in question.

The Surprising Behavior

Suppose that you establish a reference to a Shape or Cell as follows:

Set AShape = Page.Shapes(1)
Set ACell = AShape.Cells("SomeCell")

Your code can now use these references to perform various functions on the Shape and Cell objects. However if the user groups the shape (or ungroups a group that AShape belongs to) then something perhaps surprising happens. Visually, the shape in question is still on the drawing. However, behind the scenes Visio has essentially created a new shape, copied the cell formulas and properties from original shape, and deleted the original.

But your in your code, variables AShape and ACell will continue to appear to be sensible, because they continue to refer to valid chunks of data -- it's just that the data they point to is not the shape on the drawing!

This is perhaps all the more surprising if you are using these objects for events. This could entail declaring AShape or ACell using WithEvents, or alternatively using AShape.EventList or ACell.EventList. 

Either way, after group/ungroup, though the "orphaned" shape data lines on, that's not the shape that can be seen on the drawing, and hence events that happen the visible shape will not result in events that you set up on the original shape!

How to Think About It

The above behavior might strike some as unseemly or downright contrary. However, it may be best to resign oneself to the notion that Visio's developers really haven't designed Visio around addons holding on to references and handling events at the individual shape or cell level. 

Instead, Visio's in-house solutions deal with these matters as follows:

Use UniqueId to Sync Visio and Addon Data: To allow Addons to maintain extra data in conjunction with the shapes managed by Visio, use Shape.UniqueId to identify which shape goes with what Addon data -- rather than holding a Cell or Shape reference for a long time. (Yes, UniqueId is indeed copied to the "new" shape when ungroup or group occurs).

Monitor Events at Application, Document or Page level. At first glance, that would seem to encourage Visio to feed your Addon many more events than you might need or want. However, you can filter them by inspecting the event to see if it's from an object you want to handle, and in Visio 2002 you can set event filters to pre-select the events reported to your Addon code.

Undo Also Affected

It's worth noting that a similar issue affects Undo. If your code has established a reference to a shape or cell, or set an event on shape or cell, this will not be restored after a delete-undo sequence.


Article Created: 2001-08-08 Last edit: Last edit: 01-08-08 Graham Wideman
Go to:
  DiagramAntics.com