0
votes

Is there any way to select the multiples shape from slide with the same shape name.

For e.g., I have 5 shapes with the name "Textbox 60". And I want run a macro which select all the shapes from a slide named "Textbox 60". Have used the below code.

ActiveWindow.View.Slide.Shapes.Range("Textbox 60").Select
2

2 Answers

3
votes

Here's one approach:

Sub Tester()
    SelectByName ActivePresentation.Slides(1), "Textbox1"
End Sub

Sub SelectByName(sld As Slide, nm As String)
    Dim s As Shape, first As Boolean
    first = True
    For Each s In sld.Shapes
        If s.Name = nm Then
            s.Select first 'Argument determines whether to add to 
            first = False  '  existing selection, or replace it
        End If
    Next s
End Sub

You should try following @TinMan's suggestion though - that is the "better" way to go.

2
votes

Activating and Selecting Objects should be avoided whenever possible. You are better of working with the Shapes using a ShapeRange.

Sub Main()
    Dim ShapeRange As ShapeRange
    Set ShapeRange = FindShapes(ActiveWindow.View.Slide, "Textbox 60")
    If Not ShapeRange Is Nothing Then
        
    End If
End Sub


Function FindShapes(Slide As Slide, Pattern As String) As ShapeRange
    Dim Results() As Long
    ReDim Results(1 To Slide.Shapes.Count)
    Dim n As Long
    Dim Index As Long
    For Index = 1 To Slide.Shapes.Count
        With Slide.Shapes(Index)
            .Name = "Textbox 60"
            If .Name Like Pattern Then
                n = n + 1
                Results(n) = Index
            End If
        End With
    Next
    
    If n > 0 Then
        ReDim Preserve Results(1 To n)
        Set FindShapes = Slide.Shapes.Range(Results)
    End If
End Function

Note: I rewrote the code to handle multiple shapes with the same name.