Greg V
Greg V

Reputation: 141

How to get shape type in Visio using VBA?

I'm using Visio 2016 and VBA and am working with a Visio file with over 100 tabs. I need to extract the data (mainly text, connector from/to, and shape) for data processing for a processing engine.

I'm trying to figure out how to get the shape type name in Visio. For example, in a flowchart, I'm trying to figure out how to tell if a shape is a process, decision, data, etc.

Using the shape type property seems to always return 3 which appears to be visTypeShape from https://learn.microsoft.com/en-us/office/vba/api/visio.visshapetypes.

Anyone have any idea how to get the shape type with VBA? I'm also ok with parsing the XML from the OpenXML file directly but I can't find the shape name in the XML files either.

I appreciate any help in advance.

Upvotes: 1

Views: 1998

Answers (1)

Greg V
Greg V

Reputation: 141

Not sure why my question got downvoted but I just figured out the answer so I'm sharing in case someone else runs into this issue and I didn't find the answer anywhere else.

The shape.Master.Name will return the shape name, but you need to check if it's Nothing first in case it's not a shape. I didn't do that and it kept breaking the script.

This snippet will loop through all the shapes on the active page and get the ID and shape name:

Public Sub GetShapeAndID()

Dim visShape As Shape

For Each visShape In ActivePage.Shapes
    If Not visShape.Master Is Nothing Then
     Debug.Print visShape.ID & " - " & visShape.Master.Name
    End If
Next

End Sub

Upvotes: 3

Related Questions