Visio - Accessing a shapes custom Properties

Asked By Jason on 20-Feb-07 10:18 AM
This is a continuation of help I recieved on another question which I
received help, but I have now incurred a new problem. I am dropping shapes
onto a page based on a shapename I get from excel. After dropping the shape I
use code to access the shapes custom prop to place a value in it. I do this
in a For loop until I am out of shape names. My index is i. I recorded a
macro to get the code to the code to access the shape and then used code that
I received from help. When i = 1 everything works vsoshape gets set to the
shape I just dropped. The next time through the code I want vsoshape set to
the next shape I dropped but vsoshape , but it is set to "Sheet.2", then I
get an error. When i =3 then vsoShape gets set to "Sheet.3" and so on. I
don't know why this works for i=1 or what UndoScopeID1 does. Can anyone help.

For i = 1 To Shapecount
Set xlwb = .ActiveWorkbook
Set oSheet = .ActiveSheet
Shapename = Left(oSheet.Cells( i, 1), 5)
ShapeText = oSheet.Cells( i, 4)
Set mstStencil = stencil.Masters(Shapename)
ThisDocument.Pages("Oneline").Drop mstStencil, ShapePinX, ShapePinY
Dim UndoScopeID1 As Long
UndoScopeID1 = Application.BeginUndoScope("Custom Properties")
Dim vsoShape As Visio.Shape
Dim vsoCell As Visio.Cell
Set vsoShape = Application.ActiveWindow.Page.Shapes.ItemFromID(i)
Set vsoCell = vsoShape.Cells("Prop.LABEL.value")
vsoCell.FormulaU = ShapeText
Application.EndUndoScope UndoScopeID1, True
Set vsoCell = Nothing
Set vsoShape = Nothing
Next i

--
Jason V


Al Edlund replied on 20-Feb-07 12:29 PM
and does your shape have a custom property "label"?
you should consider testing first

if vsoshape.cellexists("prop.label", false) = true then

Set vsoCell = vsoShape.Cells("Prop.LABEL.value")
vsoCell.FormulaU = ShapeText

else
msgbox "custom property does not exist"
end if

al
Paul Herber replied on 20-Feb-07 04:04 PM
On Tue, 20 Feb 2007 07:18:13 -0800, Jason V


--> the Drop method above returns a shape object, this is where you
should be setting your vsoShape.


--
Regards, Paul Herber, Sandrila Ltd.          http://www.pherber.com/
Visio Utilities           http://www.visio-utilities.sandrila.co.uk/
Jason replied on 21-Feb-07 10:43 AM
Yes, my shape does have a custom property called label. After further
research I believe the problem is in the shape ID after it is placed on the
page. It appears that the first one I drop has ID(1). Since my counter i =1
it finds the shape and changes the property in the code
Set vsoShape = Application.ActiveWindow.Page.Shapes.ItemFromID(i)
However the next time through the code when i=2, the shape that was just
dropped gets an ID of 6. The code therefore does not set vsoShape to the
shape I want it sets it to "Sheet.2" . Is there a way to identify a selected
shapes ID and then I would use that instead of i  in the following statement
Set vsoShape = Application.ActiveWindow.Page.Shapes.ItemFromID(i)
Thanks
--
Jason V
Paul Herber replied on 21-Feb-07 10:57 AM
On Wed, 21 Feb 2007 07:43:01 -0800, Jason V


Your counter should be used to iterate through the number of shapes
you want to drop - this count comes from Excel, it shouyld be used
only for this and not used to reference shapes in Visio.

When you drop the shape the Visio returns a shape object, this is your
reference to the shape, you don't need anything else.

Set vsoShape = ThisDocument.Pages("Oneline").Drop mstStencil,
ShapePinX, ShapePinY

or similar



--
Regards, Paul Herber, Sandrila Ltd.         http://www.pherber.com/
Electronics for Visio        http://www.electronics.sandrila.co.uk/
Jason replied on 21-Feb-07 11:17 AM
Okay I will not use i to reference the shape. This statement works fine:
Set vsoShape = ThisDocument.Pages("Oneline").Drop mstStencil,
ShapePinX, ShapePinY
After that point I have my shape on the drawing and it is selected. Lets say
that this shape is a box and in that box I want it to say Box1 or whatever I
get from my excel spreadsheet. If my next code is this
Set vsoCell = vsoShape.Cells("Prop.LABEL.value")
vsoCell.FormulaU = ShapeText    'from excel or say box1
I get an error 424 object required. So I need to reference the shape again
with
Set vsoShape = Application.ActiveWindow.Page.Shapes.ItemFromID(?????). The
problem is I don't know the ID it gave the shape that just dropped. I assumed
it would be 1 for the first and 2 for the second that is why I thought I
could use i, but this is not true. Is there a way to find the ID of an object
just dropped?

Thanks


--
Jason V
Jason replied on 21-Feb-07 11:39 AM
I just figured it out. Thanks for the help.
I needed to put this in
ThisDocument.Pages("Oneline").Drop mstStencil, ShapePinX, ShapePinY
Set selObj = Visio.ActiveWindow.Selection
Set vsoShape = selObj(1)
idx = vsoShape.ID
Dim vsoCell As Visio.Cell
Set vsoShape = Application.ActiveWindow.Page.Shapes.ItemFromID(idx)
Set vsoCell = vsoShape.Cells("Prop.LABEL.value")
vsoCell.FormulaU = ShapeText


Thankd for the direction.
--
Jason V
Paul Herber replied on 21-Feb-07 06:40 PM
On Wed, 21 Feb 2007 08:39:02 -0800, Jason V


Please note that's not the direction that anyone here gave you.
Sorry, but that code is just waiting to fail at some time in the
future.



--
Regards, Paul Herber, Sandrila Ltd.      http://www.pherber.com/
http://www.sandrila.co.uk/