What’s My Shape’s ID?
Visio shape IDs are important.
If you are developing with Visio, you will need to know how to get at them.
We’re here to show you how!
If you are a building anything but the simplest SmartShapes, you’ll need to use Shape IDs.
If you are creating grouped shapes, you’ll need the ids to build ShapeSheet formulas that enable different group members to reference each other.
If you are writing code that makes shapes aware of each other, or establishes relationships between them, then you’ll need ids for the custom formulas that you’ll be setting programmatically.
As I was digging in to the various ways to determine a shape’s id, I uncovered five ways to obtain shape ids:
- Format > Special dialog
- Drawing Explorer window
- ShapeSheet window caption
- ShapeSheet ID function
- Automation methods
Let’s explore each method in detail.
Format > Special Dialog
This is the easiest and most reliable way to get a shape’s id through the UI.
In English versions of Visio, a quick keyboard shortcut of Alt + O, E gets me there lickety-split.
There is one problem with this, however. In recent versions of Visio (starting with Visio 2007, I believe), you might not see the “Special” option under the “Format” menu. This is because you need to be running in Developer Mode.
To turn on Developer Mode, do this:
- Select the Tools > Options menu item
- Go to the Advanced tab of the Options dialog
- Check: Run in developer mode
You will now see “Special” under the “Format” menu, and you’ll be able to quickly Alt + O, E your way to shape ids!
Drawing Explorer Window
The Drawing Explorer window can also help you to discover shape ids, but not in every case, and it isn’t 100% dependable either. But it is worth mentioning at any rate.
If you haven’t seen this window before, it is easily accessible by clicking the menu: View > Drawing Explorer Window.
There you’ll a tree that looks something like this:
Notice that I’ve expanded the leaves for Foreground Pages, Page-1, then Shapes. This shows us the names of all top-level shapes (not in groups) that are sitting on the page.
You’ll see that my page has seven shapes, three of which have “Bob” as part of their names. See the numbers at the end of some of the shape names–the ones with the “dot-number” format? This is Visio’s way of assigning unique names to all shapes on the page.
The dot-number is usually the shape id for the shape. But you can’t depend on this 100%. Let me explain…
Using the Format > Special dialog that we mentioned above, I named one of the shapes: “Bob”. Then I duplicated it twice.
The result was thee shapes named “Bob”, “Bob.6″ and “Bob.7″. The shape ids for the latter two shapes are in fact 6 and 7. But for the first shape, we get no clue as to it’s id. It is simply “Bob”, and we can’t assume that it’s id is 0 or 1 just because there is no dot-index after it’s name.
You can see a similar situation with the “Example Shape” shapes. The first one shows no index, then subsequent duplications show “.2″ and “.3″ after their names. These correspond to the shape ids, but we have no clue as to the id for the first one.
The last gotcha with the Drawing Explorer window is that some joker could name a shape, for example: “Steve.736″. It would be easy to mistake the shape id for 736, but it is very unlikely to be the case.
If we duplicate “Steve.736″, then Visio will try to bring order to the world. We won’t get “Steve.736.9″, as one might expect. Instead, Visio notices the artificial dot-index as though it created it, and creates a new shape name of “Steve.9″. Visio removes our silly “736″ from the name entirely, and appends the next available id to the new shape’s name.
If you open the ShapeSheet window, you’ll see that the window’s caption displays a bunch of information about the shape to which it belongs.
You’ll see a path-style string of information. For example, the image below shows:
What’s My Shape ID Illos.vsd:Page-1:Example Shape.3<SHAPE>
This conveniently tells us four bits of info in one compact string:
- Document name
- Page name
- Shape name
- Shape type
click to view larger image
So the shape name is “Example Shape.3″, and we can usually assume that the shape id is 3. If you draw shapes on a page using the drawing tools and don’t assign any custom names, you’ll normally just see “Sheet.3″, or “Sheet.42″.
But this method of identifying a shape falls in to two traps that I discussed up in the Drawing Explorer Window section.
Trap #1: The shape is the first with a custom name, like “Bob”. Here the ShapeSheet window also displays just “Bob” and we get no id information.
Trap #2: Some prankster assigns a “fake id” name, like “Steve.768″, even though the shape’s id is really 9. In this case, “Steve.768″ is misleading and we assume the wrong shape id.
There is a ShapeSheet function that returns the id of a shape: ID()!
You can use this function in any ShapeSheet cell. It is also helpful in debugging situations to insert this function into the text of a shape, like so:
To create the shapes above, I did this:
- Double-click a rectangle to get into text-edit mode (F2 will do this as well)
- Bring up the Insert > Field dialog
- Select: Custom Formula in the Category list
- Enter: = ID()
- Close the window
Your shape will now display it’s own id. Copies of the shape will subsequently show their ids, as you can see in the image above!
Automation & Programming
There are a couple of automation methods that help you to get at a shape’s id.
- Shape.ID will return just the shapes id, as a number.
- Shape.NameID will return the shape’s “Sheet ID”, which is helpful in building formulas that reference other shapes.
The VBA procedure below shows you a simple example of how you can use these methods to extract id information from shapes.
Public Sub PrintShapeID Dim shp As Visio.Shape '// Get a Visio shape from the selection: Set shp = Visio.ActiveWindow.Selection(1) '// Get a Visio shape from the page: 'Set shp = Visio.ActivePage.Shapes(1) '// Output NameID and ID Debug.Print shp.NameID Debug.Print shp.ID '// Example results: '>> Sheet.36 '>> 36 End Sub