Select all similar shapes in Microsoft Visio

5

2

How do I select all similar shapes in a diagram? For example, how do I select all arrows, or all rectangles?

Ryan Fernandes

Posted 2010-02-23T12:27:31.850

Reputation: 245

Not really a programming question IMO – None – 2010-02-23T12:50:53.007

Answers

2

You can do it in VBA, assuming the arrows or rectangles were created using a stencil, rather than just drawn. This code will select all shapes in the active page like the selected shape (using the shapes master)

Sub SelectSimilarShapesByMaster()
    If ActiveWindow.Selection.Count = 0 Then Exit Sub

    Dim SelShp As Visio.Shape
    Set SelShp = ActiveWindow.Selection(1)

    If SelShp.Master Is Nothing Then Exit Sub
    ActiveWindow.DeselectAll
    Dim CheckShp As Visio.Shape
    For Each CheckShp In ActivePage.Shapes
        If Not CheckShp.Master Is Nothing Then
            If CheckShp.Master = SelShp.Master Then
                ActiveWindow.Select CheckShp, visSelect
            End If
        End If
    Next CheckShp

End Sub

You can also mess with looking at the shapesheet geometry sections for shapes to see if they're rectangles like so:

Sub SelectRectangles()
    If ActiveWindow.Selection.Count = 0 Then Exit Sub

    Dim SelShp As Visio.Shape
    Set SelShp = ActiveWindow.Selection(1)

    ActiveWindow.DeselectAll
    Dim CheckShp As Visio.Shape
    For Each CheckShp In ActivePage.Shapes
        If IsRectangle(CheckShp) Then ActiveWindow.Select CheckShp, visSelect
    Next CheckShp

End Sub

Function IsRectangle(TheShape As Visio.Shape) As Boolean
    Dim Width As Double, Height As Double
    Width = TheShape.CellsU("Width")
    Height = TheShape.CellsU("Height")
    Dim Result As Boolean
    Result = (TheShape.RowCount(visSectionFirstComponent) = 6)
    Result = (Result And TheShape.CellsSRC(visSectionFirstComponent, 1, 0).ResultIU() = 0 And TheShape.CellsSRC(visSectionFirstComponent, 1, 1).ResultIU() = 0)
    Result = (Result And TheShape.CellsSRC(visSectionFirstComponent, 2, 0).ResultIU() = Width And TheShape.CellsSRC(visSectionFirstComponent, 2, 1).ResultIU() = 0)
    Result = (Result And TheShape.CellsSRC(visSectionFirstComponent, 3, 0).ResultIU() = Width And TheShape.CellsSRC(visSectionFirstComponent, 3, 1).ResultIU() = Height)
    Result = (Result And TheShape.CellsSRC(visSectionFirstComponent, 4, 0).ResultIU() = 0 And TheShape.CellsSRC(visSectionFirstComponent, 4, 1).ResultIU() = Height)
    Result = (Result And TheShape.CellsSRC(visSectionFirstComponent, 5, 0).ResultIU() = 0 And TheShape.CellsSRC(visSectionFirstComponent, 5, 1).ResultIU() = 0)
    IsRectangle = Result
End Function

Hopefully that'll at least get you started...

Jon Fournier

Posted 2010-02-23T12:27:31.850

Reputation: 525

Fantastic. That first sub did exactly what I (and, I believe, the OP) wanted! Thanks. – hajamie – 2015-04-30T17:01:13.900