Visio Guy

Visio Discussions => Programming & Code => Topic started by: Visisthebest on October 20, 2023, 09:40:05 AM

Title: GetShapesContainedByThisShape() VBA to quickly get shapes 'contained' by shape
Post by: Visisthebest on October 20, 2023, 09:40:05 AM
Based on the very useful code Wapperdude published here:
http://visguy.com/vgforum/index.php?topic=7868.0

I changed it for the purpose of some VBA to quickly get a selection of shapes contained by 1 shape I select.

Contain means all shapes which are in the area of the selected shape and are fully surrounded (on all sides) by this shape. Shapes partially contained are not selected.

(To be sure, this works for all shapes not just actual containers.)

This works like a charm very useful thank you Wapperdude!


Sub GetShapesContainedByThisShape()

    Dim ContainShape As Visio.shape
   
    If ActiveWindow.Selection.Count <> 1 Then
    MsgBox ("Please select 1 shape")
    Exit Sub
    End If
   
    Set ContainShape = ActiveWindow.Selection(1)
    Dim ShapesContainedByContainShape As Visio.Selection
    Set ShapesContainedByContainShape = ContainShape.SpatialNeighbors(visSpatialContain, 0.01, 0)
   
    ActiveWindow.DeselectAll
   
    Dim LoopShape As Visio.shape
    For Each LoopShape In ShapesContainedByContainShape
        ActiveWindow.Select LoopShape, visSelect
    Next LoopShape

End Sub