Objective
To change the names of shapes on the PowerPoint slide using VBA excel.
Approach
Here we are changing the names of the rectangular shapes based on text inside the shape. For demonstration purpose, we had 3 rectangles and 1 command button placed on slide 1. All 3 rectangles and command button had auto generated assigned names before executing the VBA code.
Code
Here the code loops through each shape on slide 1.First, it checks whether the selected shape is a rectangle or not.If this condition is satisfied then it checks that the text inside the shape starts with the string “Revenue” or not.If both the conditions are satisfied then it will change the name of the shape.
Sub NameShapesOnSlide() Dim MyShape As Shape Dim ShapeIndex As Integer ShapeIndex = 1 'Looping through each shape in slide 1 For Each MyShape In ActivePresentation.Slides(1).Shapes 'Check if the shape is a rectangle If MyShape.AutoShapeType = msoShapeRectangle Then 'Shape should contain the text starting with "Revenue" If Left(MyShape.TextFrame.TextRange.Text, 7) = "Revenue" Then 'Change the name of the shape MyShape.Name = "MyShapeName " & ShapeIndex End If 'Increase the shape index for next shape ShapeIndex = ShapeIndex + 1 End If Next MyShape End Sub
We can see in the image below that before executing the VBA code, rectangular shapes had name as Rectangle 1, Rectangle 2, Rectangle 3.
Once we had executed the VBA code, names of rectangular shapes have changed to MyShapeName 1 ,MyShapeName 2, MyShapeName 3.We can check the names in “Selection Pane” in format tab.We can see the same in the image below, on the right side of the image we can see the changed names.
Reference
https://docs.microsoft.com/en-us/office/vba/api/powerpoint.shape.name
Post you may like