Accessing a shapes custom Properties

J

Jason V

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
 
A

Al Edlund

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
 
P

Paul Herber

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

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

Jason V

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)
Set vsoCell = vsoShape.Cells("Prop.LABEL.value")
vsoCell.FormulaU = ShapeText
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
 
P

Paul Herber

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

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
 
J

Jason V

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
 
J

Jason V

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

Paul Herber

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top