Question

Is there a way in Visio VBA to see if there is a shape in front of or behind a shape in Visio?

I imagine I could write something that checks the bounding box of each shape in a page to see if it occupies the same space as my shape. I'd rather use something built-in since checking each shape could take a long time as a drawing gets more and more shapes.

Was it helpful?

Solution

The Shape.SpatialRelation property will tell you if two shapes touch. The Shape.Index property will tell you which is in front or behind in the z-order.

Here is a simple example:

Public Sub DoShapesIntersect(ByRef shape1 As Visio.Shape, ByRef shape2 As Visio.Shape)

    '// do they touch?
    If (shape1.SpatialRelation(shape2, 0, 0) <> 0) Then

        '// they touch, which one is in front?
        If (shape1.Index > shape2.Index) Then
            Debug.Print shape1.Name + " is in front of " + shape2.Name
        Else
            Debug.Print shape1.Name + " is behind " + shape2.Name
        End If
    Else
        Debug.Print "shape1 and shape2 do not touch"
    End If

End Sub

Read more here:

Shape.SpatialRelation Property on MSDN

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top