Visio Guy

Visio Discussions => Programming & Code => Topic started by: dlFHG on August 21, 2014, 05:44:26 PM

Title: Selecting shape by shape data identifier
Post by: dlFHG on August 21, 2014, 05:44:26 PM
Hello everybody.  As a newbie to Visio I'm having some difficulty selecting a shape by its external shape data.  In the first column of the external data I have a column headed "Shape ID" which holds unique identifiers for parts in my system.  On the drawing there is only one shape per "Shape ID".  I would like to cycle through each of the shapes using a for loop and select a specific one by the value in the field "Shape ID". 

If anyone is able to help me with this, could you provide both a generic example of selecting a shape based on a field in external data and specific example of the problem outline above?  Many thanks!
Title: Re: Selecting shape by shape data identifier
Post by: Yacine on August 21, 2014, 08:55:15 PM
Make sure to get the right spelling of the ID field.
Get one linked shape, open its shapesheet and check in the custom properties section for the right field.
It's the uppermost left column that holds the name.

in VBA you would then get the shape by checking if
shp.cells("prop." & YOURFIELDNAMEHERE).resultstr(visnone)
Title: Re: Selecting shape by shape data identifier
Post by: dlFHG on August 22, 2014, 03:48:32 PM
Yacine, thank you for your response. I tried to plug in the line of code you gave me and it's returning an error saying that the field is empty for my Shape ID.  I'm certain that I have the spelling right and a value in the field.  Even if I delete underscore before the name (which Visio automatically put there when it named the field) it still returns as an empty value.  I've attached a snapshot of the few lines of code to give context and the Define Shape Data box with all the properties.  Am I calling the property correctly?
Title: Re: Selecting shape by shape data identifier
Post by: Yacine on August 23, 2014, 07:24:27 AM
You definitely need to check first that the selected shape has an according field.
The code needs a line like:

debug.print "myID: "; str(ID)
For Each shp In ActivePage.Shapes
    If shp.CellExists("prop._VisDM_Shape_ID", visExistsAnywhere) Then
        debug.print "shapeID: "; shp.Cells("prop._VisDM_Shape_ID", visSectionProp).ResultStr(visNone)
    End If
Next shp
Title: Re: Selecting shape by shape data identifier
Post by: Jumpy on August 25, 2014, 07:09:36 AM
dlFHG:

When in the VBA editor go to the first line of the editor window and add:

Option Explicit

With that the interpreter/compiler would have warned you, that it interprets the _VisDM_Shape_ID in your code as a variable and an empty one at that. As you can see in Yacines ansrer, you need it to be a string (see above) or a variable:


Dim mySection as String, myCell as String, myID as String
mySection="Prop"
myCell="_VisDM_Shape_ID"
myID=shp.Cells(mySection & ." & maCell).ResultStr("")
'...
Title: Re: Selecting shape by shape data identifier
Post by: dlFHG on August 29, 2014, 07:37:43 PM
Thanks Yacine and Jumpy, the problem was outside of the function.  Once I corrected the issue with shp.id everything ran smoothly.  I'll definitely start employing the use of Option Explicit