Visio Guy

Visio Discussions => Programming & Code => Topic started by: Toxicboumboum on March 26, 2018, 02:12:59 PM

Title: Get Shape properties
Post by: Toxicboumboum on March 26, 2018, 02:12:59 PM
Hello everyone,
I'd like to know if there is a way to get the shape proprety? for exemple, is it possible to get the cost of my shape?
Like for text where I have to do: Shape.Text

Thanks for your help

Title: Re: Get Shape properties
Post by: Yacine on March 26, 2018, 03:41:44 PM
shape.cells("prop.cost").resultstr("") to read it
shape.cells("prop.cost").formulau to write it.
Title: Re: Get Shape properties
Post by: dpetrov on September 12, 2019, 03:00:47 PM
Hi guys,

is there anyway possible I can list all the prop.* like a Key/values fashion?

I just want to see all the properties defined for a shape (considering the fact I don't know what properties could have been configured)

Hope that makes sense.
Title: Re: Get Shape properties
Post by: Nikolay on September 15, 2019, 07:32:58 PM
Yes, it does, but no, it is not possible  :)
Title: Re: Get Shape properties
Post by: wapperdude on September 15, 2019, 09:07:02 PM
???

Should be possible. Requires using code.  Select shape, select the shape section, loop thru the rows pulling out the names of the rows.
Title: Re: Get Shape properties
Post by: Nikolay on September 15, 2019, 09:11:43 PM
Let me clarify. I mean, getting property values using something like "prop.Salary" is not possible.
Getting it using the construction provided by Yacine is of course possible. Like,
shape.cells("prop.Salary").resultstr("")
Title: Re: Get Shape properties
Post by: wapperdude on September 15, 2019, 09:35:31 PM
Sorry.   :-[  Misunderstood.   :o
Title: Re: Get Shape properties
Post by: dpetrov on September 17, 2019, 06:36:43 PM
Thanks everyone for your feedback.

I've actually managed to achieve this, but I am not necessarily proud of the way I've done it .. :D

In this case it will show me 3 of the properties (tbh, I have 4 but I don't quite understand how to obtain the 1st one - element 0 doesn't seem to be available?)

The function will simply return a 2  dimensional array with key/values (property label/values) which can be looped through later-on.

Here's the code .. Not terrible, not great ... ;)

Private Function ListShapeProperty(ByVal shape_ As Shape) As Variant
    Dim i As Integer
    Dim Count As Integer
   
    On Error GoTo End_
    Debug.Print ("BEGIN")
    Dim properties(1 To 3, 1 To 2) As Variant
   
    For i = 1 To 3
       
        Debug.Print (i)
        Label = (shape_.CellsSRC(VisSectionIndices.visSectionProp, i, VisCellIndices.visCustPropsLabel).Formula)
        value = (shape_.CellsSRC(VisSectionIndices.visSectionProp, i, VisCellIndices.visCustPropsValue).Formula)
        properties(i, 1) = Label
        properties(i, 2) = value
        Debug.Print (Label & " " & value)
    Next i

    Debug.Print ("FINISH")
   
End_:
    ListShapeProperty = properties

End Function
Title: Re: Get Shape properties
Post by: Nikolay on September 17, 2019, 08:39:19 PM
I would still recommend you follow Yacine proposal; don't use . Formula to get the value, it may cause troubles in case value is not a string or is a calculated value (uses fields for example)
Title: Re: Get Shape properties
Post by: dpetrov on September 17, 2019, 09:32:09 PM
But the problem is, Nikolay, that I don't know the names of the properties... So this function actually will return a 2 dimensional array with the property name & it's value. Yacine's method assumes you know the property name already (shape.cells("prop.cost").resultstr("")). Unless I totally missed the point here? :-)
Title: Re: Get Shape properties
Post by: wapperdude on September 18, 2019, 01:20:16 AM
@dpetrov:  Your code approach is basically the way to do this.

Wrt to Section rows, it's a 0-based system, not a 1-based system.
You can find the total number of rows, and then iterate from 0 to rowcount-1.  Possible code:


intRows = vsoShape.RowCount(Visio.visSectionProp)

For intCounter = 0 To intRows - 1
        Debug.Print (i)
        Label = (shape_.CellsSRC(VisSectionIndices.visSectionProp, i, VisCellIndices.visCustPropsLabel).Formula)
        value = (shape_.CellsSRC(VisSectionIndices.visSectionProp, i, VisCellIndices.visCustPropsValue).Formula)
        properties(i, 1) = Label
        properties(i, 2) = value
        Debug.Print (Label & " " & value)
Next intCounter
Title: Re: Get Shape properties
Post by: dpetrov on September 18, 2019, 08:20:08 AM
Ah thanks dude!

We seem to have ended-up with a somewhat proper solution now :-) Here is what I worked out and it seems to do the job!

Private Function ListShapeProperties(ByVal shape_ As Shape)
    On Error GoTo err_
       
    intRows = shape_.RowCount(Visio.visSectionProp)
   
    Dim i As Integer
    Dim properties() As String
    ReDim properties(0 To intRows - 1, 1 To 2)
   
    Debug.Print ("Property Name : Property Value")
   
    For i = 0 To intRows - 1
        PropName = Replace((shape_.CellsSRC(VisSectionIndices.visSectionProp, i, VisCellIndices.visCustPropsLabel).ResultStr("")), Chr(34), vbNullString)
        PropValue = Replace((shape_.CellsSRC(VisSectionIndices.visSectionProp, i, VisCellIndices.visCustPropsValue).ResultStr("")), Chr(34), vbNullString)
       
        properties(i, 1) = PropName
        properties(i, 2) = PropValue
       
        Debug.Print (PropName & ": " & PropValue)
    Next i
   
err_:
    ListShapeProperties = properties
   
End Function


Edit: The code has been edited as per Nikolay's post.
Title: Re: Get Shape properties
Post by: Nikolay on September 18, 2019, 03:32:09 PM
let me clarify. What I meant is that you get property value like Yacine wrote. Instead of:

        PropValue = shape_.CellsSRC(VisSectionIndices.visSectionProp, i, VisCellIndices.visCustPropsValue).Formula

Use:

       PropValue = shape_.CellsSRC(VisSectionIndices.visSectionProp, i, VisCellIndices.visCustPropsValue).ResultStr("")

I would not recommend to use "formula" to get value of a property. Use either "ResultStr" or "Result", like Yacine suggested.
Title: Re: Get Shape properties
Post by: dpetrov on September 18, 2019, 06:27:25 PM
Ah, I got you. Thanks folks!
Title: Re: Get Shape properties
Post by: wapperdude on September 18, 2019, 07:17:08 PM
Oops.  Good catch Nikolay.  My apologies for missing that.