Share:

" /> Visio Guy » What’s My Shape’s ID?
Home » Code, ShapeSheet

What’s My Shape’s ID?

Submitted by on July 15, 2009 – 10:50 am | | 33729 views 10 Comments

shape-id

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:

  1. Format > Special dialog
  2. Drawing Explorer window
  3. ShapeSheet window caption
  4. ShapeSheet ID function
  5. 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.

format-special-dialog

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:

  1. Select the Tools > Options menu item
  2. Go to the Advanced tab of the Options dialog
  3. 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:

drawing-explorer-shape-ids

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

Have a look for yourself:shapesheet-id

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:

id-shapesheet-function

To create the shapes above, I did this:

  1. Double-click a rectangle to get into text-edit mode (F2 will do this as well)
  2. Bring up the Insert > Field dialog
  3. Select: Custom Formula in the Category list
  4. Enter: = ID()
  5. 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

10 Comments »

  • [...] your group has an ID of 5. This means your group can be referenced by the name [...]

  • [...] order for a sub-shape to refer to a cell in another shape’s ShapeSheet, you need to know the id of the other shape. So let’s get the id of the group shape first. Simply select your tree-group-shape, then [...]

  • 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:

    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
    
    
  • 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.

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.

*