Visio shape IDs are important.
No, seriously!
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.
The Format > Special Dialog (Shape Name Dialog as of Visio 2010)
This is the easiest and most reliable way to get a shape’s id through the UI.
Starting with Visio 2010, you need to have the Developer Tab visible. See: How to Show the Developer Ribbon Tab (and Why). You can then bring up the Shape Name dialog via Developer > Shape Design > Shape Name.
In older versions, you (might) need to be running in Developer Mode. Once that’s activated, the keyboard shortcut Alt + O, E (for English versions of Visio) gets you to the Special dialog. You can also follow the menus: Format > Special.
The dialog is essentially the same, regardless of how old or new your version of Visio is.
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.
ShapeSheet Window
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.
ShapeSheet Function
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.
[code lang=”vb” light=”true”]
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
[/code]
Eric says
I have two groups one called MainTitleBlock and the other RevisionBlock Each has a Shape that I have labled DrwaingNumber. The MainTitleBlock.DrawingNumber has editable text and I have the users type in the drawing number. I want this to repeat in the Revision Groups Drwaing number. In the shape sheet Textfield Section(Value) The only way I can get this to work right now is directly identifying the shape by sheet number ie.
=Guard(shapetext(sheet.12785!theText))
I want to have it directly repeat the text via the shape name. Ie. Shapetext(MainTitle.DrawingNumber!TheText).
I have tried several methods yet I keep getting error in formula in the Revision Drawing Textfields Value Cell.
the groups mainTitleblock and RevisionBlocks are master shapes.
What is annoying when the users Complete a drwaing and insert he borders the two drwaing number shapes are not repeating each other and I have to enter in the TEXT Fields Value block GUARD(ShapeText((SHEET.12785!TheText))) or what ever the maintitleBlock Drwaing numebr sheet is per drawing.
M A RAHAMAN says
Hello Sir,
I WANT TO REPRODUCE THE SAME VISIO DRAWING DRAWN USING THE VISIO S/W ON TO THE BROWSER,BY READING THE .VDX FILE IN A JAVASCRIPT OF THE VISIO DRAWING.SUPPOSE I DONT HAVE VISIO ON MY SYSTEM EVEN THEN I SHOULD BE ABLE TO OPEN THE VISIO DRAWING ON THE BROWSER.NOT BY CONVERTING THE VISIO DRAWING(.VDW)TO A JPEG FILE AND THERE BY DISPLAYING IT ON BROWSER.
PLEASE TELL ME HOW TO DO IT.
i.e
BUT BY READING EACH VISIODOCUMENT>MASTERS>MASTER>SHAPE{0,1,2…N} XFORM COORDINATES AND THERE by DRAWING THE SPECIFIED SHAPE(RECTANGLE/LINE/TRIANGLE etc..) AT THOSE SPECIFIED COORDINATES.I HAVE JQUERY LIBRARIES TO DRAW BASIC DIAGRAMS LIKE LINE,RETCANGLE,DECISION BOX etc.. BY WHICH I CAN CALL THOSE FUNCTIONS TO DRAW THE SHAPES.
I AM NOT ABLE TO FIND OUT THE RELATION BETWEEN MASTER>SHAPE AND PAGE>SHAPE AND ALSO GROUP OF SHAPES SO AS TO DRAW THE FIGURE.
PLEASE HELP ME OUT TO CODE THE ABOVE QUERY.
IF ANY API IS AVAILABLE PLEASE PROVIDE IT.
THANKS IN ADVANCE.
Visio Guy says
Hi M.A.R.,
Your question is way too difficult to be answered in a comment, and what you are trying to do is basically re-create the Visio rendering engine using javascript.
You might start with this article http://www.visguy.com/2006/11/30/visio-and-xml-conference-resources/.
There is probably an id that points to a master for each shape that comes from a master. Then you can look up the master by id, and extract more of the graphical information from there.
M A RAHAMAN says
Thanks for the Reply.
1>Sir,I AM NOT ABLE TO DIFFERENTIATE A SHAPE(i.e Which Shape’s is this Either a Rectangle/Decision/Data/Triangle/Line).Please tell me how to do that.Is it by using Master Shape ID or Page Shape ID.
2>CONNECTIONS WERE GIVEN FOR EACH SHAPE EXCEPT FOR ONE DIMENSIONAL FIGURES.SO PLEASE TELL ME HOW TO CONNECT THE SHAPES OF THE PAGE.
RECTANGLE HAS 4 CONNECTIONS SO WHICH SIDE IS (1,2,3,4).I AM NOT ABLE TO GET IT.
3>VISIO DRAWS DIAGRAMS WITH ORIGIN AT THE BOTTOM LEFT CORNER.AS I AM TRYING TO DRAW THE VISIO DIAGRAM ON BROWSER IT IS GETTING REVERSED.I AM CONVERTING INCHES TO PIXELS AND DRAWING.
SO PLEASE TELL ME HOW TO DRAW IT.
THANKS IN ADVANCE.
Visio Guy says
Hi MAR,
This isn’t really the place for this discussion. Please try our forums at http://www.visguy.com/vgforum.
Also, you might try not using ALL CAPS because it is perceived as yelling, and you are likely to not get the answers you are looking for.
Cheers,
Chris
Joel says
Thank you for the post. However is there a way to read all Shape ID’s from a page and change the protection property?
Visio Guy says
Hi Joel,
Here is a routine that outputs all shape ids on a page, and locks the width of the shape:
[code]
Public Sub ShapeIdsEtc(ByRef visPg As Visio.Page)
Dim shp As Visio.Shape
For Each shp in visPg.Shapes
‘// Dump the shape’s id:
Debug.Print shp.ID
‘// Lock the width of the shape, using the ShapeSheet:
shp.CellsU("LockWidth").ResultIU = 1
‘//…1 = true, not-1 = false in the ShapeSheet.
Next shp
‘// Cleanup:
Set shp = Nothing
End Sub
[/code]
Hoss says
Thank you for this post. I ran into a problem with shape id’s. After a few copy and delete I get an ID more than the max limit for 16bit integer ID (ID16). Many of Visio functions like SetFormulas, GetFormulas, and etc. need a short ShapeID and when converting the Shape.ID to short it will become a negative number. Is there a way to reset the ID. Or is there a workaround for long integer id’s to be used in above mentioned functions. Thanks.
Edio Favaro says
Hi, Visio Guy.
I hope you can help me. I have several flowcharts, and some are pretty big. I need to show the ID number for any process in the flowchart. The ID number must be positioned in the top-left corner of any process shape. This circle must be seen in the visio file and in the printed flowchart. How can i do this?
Visio Guy says
Hi EF,
You should be able to create a data graphic that does this. Do your shapes have a shape data field for the ID?
Or are you talking about numbering the steps in the process? In that case, try the number shapes add-on. You’ll find it under:
View > Macros group > Add-Ons > Visio Extras > Number Shapes…
EDIO RENATO FAVARO says
Hi, it worked. Thank’s!
Ray Merrill says
Hi VG,
When I use a hyperlink to link one shape to another on a different page, Visio does not highlight or select that shape when it gets to the other page. It just drops me off at the page. The “Off Page Reference” shape is the only one that seems to work this way. How can I hyperlink to a shape and have it (the destination shape) highlight, or otherwise indicate that the link was specifically to that shape?
Thanks
Ray