Zahid Khan
Zahid Khan

Reputation: 81

Select similar shape type in Powerpoint using VBA

I was trying to create a VBA script for my PowerPoint where it will recognize the selected shape type and select all similar shape type available in current slide, similar to the select all shape with similar colors.

For eg: If there is multiple shape type in a single style like msoRectangle, msoOval, msoTriangle, etc etc., and If I select msoRectangle and click on the macro button it should select all the msoRectangle shape available in a slide.

I have tried to write the code below but it's not selecting the shape it's selecting only the last shape created.

Sub SelectSimilarShapes()
    Dim selectedShape As Shape
    Dim currentSlide As Slide
    Dim shapeType As MsoAutoShapeType
    Dim shp As Shape
    
    ' Get the selected shape
    Set selectedShape = ActiveWindow.Selection.ShapeRange(1)
    Set currentSlide = selectedShape.Parent
    
    ' Get the type of the selected shape
    shapeType = selectedShape.AutoShapeType
    
    ' Loop through all shapes on the slide
    For Each shp In currentSlide.Shapes
        ' Check if the shape type matches the selected shape
        If shp.Type = msoAutoShape And shp.AutoShapeType = shapeType Then
            shp.Select msoTrue ' Select the shape
        End If
    Next shp
End Sub

Where am I going wrong, and what's wrong in this script?

Upvotes: 0

Views: 78

Answers (0)

Related Questions