Reading a number from a shape data field with VBA

Started by Visisthebest, April 24, 2020, 01:01:15 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Visisthebest

Hi everyone,

This is a beginner question, browsed the forum and the Visio VBA reference of Microsoft but couldn't find a clear answer to a simple question about reading shape data.

I have a shape that has some number values as shape data, these are simple integer numbers. No problem finding these shape data field and testing with CellExists(U) whether they exist, but reading the numbers from the fields with VBA does bring up the question about the cleanest way to do this.

There are so many different Result... properties on the Cell object but which one is best to use to just read an integer from a shape data field? No units like inches/mm etc applicable I just need a number to fill a VBA integer/long variable.

Thank you for your guidance with this beginner question!
Visio 2021 Professional

Paul Herber

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

Visisthebest

Thank you Paul yes I saw that property but it requires me to specify UnitsNameOrCode which has these constants as options:
https://docs.microsoft.com/en-us/office/vba/visio/concepts/about-units-of-measure-visio

but I have numbers in shape data that aren't in any of these units, so what effect will it have to specify a measure of unit?

Visio 2021 Professional

wapperdude

There are several ways to get "integer" values.  The code below demonstrates two of these approaches.  The 1st uses ResultInt() .  For the units declaration, you can use visNone.

The 2nd approach just uses Result().  Since the variable holding the value is declared as an integer, this works.  The stored value in User.R1 was not an integer.

Strictly speaking, the 1st approach, probably, is technically better.


Sub GetInt()
    Dim mV1 As Integer
    Dim mV2 As Integer
    Dim mShp As Visio.Shape
   
    Set mShp = ActiveWindow.Selection(1)
    mV1 = mShp.Cells("Width").ResultInt(visNone, 0)
    Debug.Print mV1
   
    mV2 = mShp.Cells("User.R1").Result(visNone)
    Debug.Print mV2
End Sub

Visio 2019 Pro

Visisthebest

Thank you Wapperdude clear for me now, really good too know about the visNone constant would be better if Microsoft lists it in the unit constants overview (in case you don't want to apply a unit of measure):
https://docs.microsoft.com/en-us/office/vba/visio/concepts/about-units-of-measure-visio

Shape data + VBA provides amazingly powerful possibilities Visio is an amazing software tool!
Visio 2021 Professional