Get the Value of a shape data field / property

Started by Herokuretsu, May 21, 2015, 02:42:38 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Herokuretsu

Hi to everyone,

Really new to Visio and programming on it with VBA.
I am trying something that seems very simple but I have been struggling for a day now.

I have shapes in my drawing that contain 3 extra fields with values.
These fields have been introduced by linking to a database but the link has been severed and now the data is static in the drawing.

I am trying to retrieve the value for a specific field for each shape in the drawing.

So far the code I have been using is similar to what I have seen in the forum but it seems that it cannot find the specific field or property (I am getting the unexpected end of file error).

Here is my code:

Public Sub IterateThroughShapes()
Dim VisioPage As Page
Dim VisioShape As Shape
Dim propval As String

'Set the reference to the active page
Set VisioPage = Application.ActivePage

'Loop through all the shapes in the active page
For Each VisioShape In VisioPage.Shapes
   
    propval = VisioShape.Cells("Prop.ServiceNodeDescription").ResultStr(visNone)
   
    MsgBox propval

Next VisioShape

End Sub

As you see the field that was one of the columns in the table I had linked is called ServiceNodeDescription.
I can see it perfectly when I visualise it with Data Graphics and there are values for all the shapes but I cannot retrieve it.
I am using Visio 2013 Professional.

Any help would be very much appreciated.
Thank you all.

Paul Herber

Does every shape have that Prop cell? If not then it will give an exception on the first one that doesn't. I think you need either an error trap or a .CellExists() test.
Electronic and Electrical engineering, business and software stencils for Visio -

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

Herokuretsu

Hi,

Thank you for your reply.
Let me correct myself.
I don't know if I am accessing the property correctly.
Is it supposed to be a Prop?

And yes all the shapes have the Shape Data with the field ServiceNodeDescription.

My guess is that I am not accessing it properly and I should be using some kind of different method.

Any input is more than welcome as it is getting rather frustrating.

Thank you.

JohnGoldsmith

Hi,

All Shape Data row names are prefixed with "Prop." and the default cell is the Value one so you don't need to specify it. 

As Paul says you need to add some handling to check that a cell exists (for sections with variable rows) before you access it.  So to expand your code and mine (judging by the comments), how about something like this:

Public Sub IterateThroughShapes()
Dim VisioPage As Page
Dim VisioShape As Shape
Dim propval As String

Const C_TargetCellName As String = "Prop.ServiceNodeDescription"

'Set the reference to the active page
Set VisioPage = Application.ActivePage

'Loop through all the shapes in the active page
For Each VisioShape In VisioPage.Shapes
    If VisioShape.CellExistsU(C_TargetCellName, visExistsAnywhere) <> 0 Then
        propval = VisioShape.CellsU(C_TargetCellName).ResultStr(visNone)
        MsgBox propval
    End If
Next VisioShape

End Sub


Does that help?

Best regards

John
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/

wapperdude

A point of clarification regarding the need for cell exists testing..iterating thru all shapes on a page means all, both 2D and 1D.   So the loop finds line, connectors, and every sort of 2D shape.

HTH
Wapperdude
Visio 2019 Pro

Paul Herber

... and annotations, guides, images and containers.

Also check that your shapes are not groups with the Prop data being in one of the sub-shapes.
Electronic and Electrical engineering, business and software stencils for Visio -

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

JohnGoldsmith

And if you just want to retrieve all of the values for Shapes containing ServiceNodeDescription then an alternative might be to run a report (see Shape Reports on the Review tab).  I've attached an image as an example.

Best regards

John
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/