VBA: Change One Shape to Another in PowerPoint

Created: Wednesday, November 29, 2017, posted by Geetesh Bajaj at 9:30 am

Learn how to use VBA to change all instances of one shape to another without having to manually select anything, thanks to this code from Steve Rindsberg.


This one started as a forum post from Manon Mikkers Minning on the Presentation Guild site. Forum access is only available to Presentation Guild members.

Here’s the problem scenario. A particular slide had many trapezoids that should have been rectangles. Normally, the solution is to select all trapezoids and use PowerPoint’s Change Shape option to turn them into rectangles. But assuming you have hundreds of trapezoids on one or more slides, it can be a boring, time-consuming task to select all of them. And then, of course, they need to be changed to rectangles.

Would VBA provide an easier and more elegant solution? Steve Rindsberg of PowerPoint FAQ and PPTools has an answer.

Trapezoids to Rectangles

First you need to know how you can run VBA scripts in PowerPoint — then use this code. Explore the code a bit though since Steve has put in some helpful comments so that you can edit some values as required.

Sub ChangeOneShapeToAnother()
 
Dim oSl As Slide
Dim oSh As Shape
Dim lFromType As Long
Dim lToType As Long
 
' Change these if you want to change from/to
' other shape types:
lFromType = msoShapeRectangle
lToType = msoShapeTrapezoid
' Press F2 in the VBA editor then search on AutoShapeType
' to see what the other possible autoshape types are
 
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.Type = msoAutoShape Then
If oSh.AutoShapeType = lFromType Then
oSh.AutoShapeType = lToType
End If
End If
Next
Next
 
End Sub

This code worked for Manon, but it ignored all the trapezoids that were part of groups in PowerPoint. So Manon found some code on Steve’s own site and modified it so as to work for shapes within groups!

 
Sub ChangeOneShapeToAnother()
 
Dim oSl As Slide
Dim oSh As Shape
Dim lFromType As Long
Dim lToType As Long
Dim X As Long
 
' Change these if you want to change from/to
' other shape types:
lFromType = msoShapeTrapezoid
lToType = msoShapeRectangle
' Press F2 in the VBA editor then search on AutoShapeType
' to see what the other possible autoshape types are
 
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
 
With oSh
Select Case .Type
Case Is = msoGroup
For X = 1 To .GroupItems.Count
If .GroupItems(X).Type = msoAutoShape Then
If .GroupItems(X).AutoShapeType = lFromType Then
.GroupItems(X).AutoShapeType = lToType
End If
End If
Next X
End Select
End With ' oSh
Next oSh
Next oSl
End Sub

And now Manon could even change shapes within groups! Manon adds, “All I did was adapt the code that Steve gave me, in the way shown on his page. It took some puzzle-solving, but the puzzle pieces are all Steve’s. I think this is pretty simple code for Steve actually.”

Steve does mention on his site, “This code won’t look at groups within groups.”

Once you have placed this code in the VBA editor, close the window to get back to your PowerPoint window. Before you proceed further, it’s a great idea to save your file — be sure to save as a PowerPoint Macro-Enabled Presentation with the PPTM file extension. If you save as any of the other file formats, PowerPoint will offer to remove the macros, and then, of course, the code to change one shape to another will not work! See our PowerPoint File Formats page to learn about these file formats.

Then open or switch to the presentation with the shapes you want to change and run the macro. To do that, press the Alt F8 shortcut key. In the Macro dialog box that appears, next to Macro in: choose All open presentations, as shown in the figure below. Click the ChangeOneShapeToAnother Macro name, and then click the Run button.

ChangeOneShapeToAnother Macro

The code looks any trapezoid shapes in your slides and changes them all to rectangles. You can substitute the trapezoid and rectangle shapes to any shapes of your choice. Of course, you will need to know the VBA names of these shapes. Here are some common shape names:

Bevel: msoShapeBevel
Can: msoShapeCan
Chevron: msoShapeChevron
Corner, Folded: msoShapeFoldedCorner
Cross: msoShapeCross
Cube: msoShapeCube
Diamond: msoShapeDiamond
Donut: msoShapeDonut
Heart: msoShapeHeart
Hexagon: msoShapeHexagon
Moon: msoShapeMoon
Octagon: msoShapeOctagon
Oval: msoShapeOval
Parallelogram: msoShapeParallelogram
Pentagon: msoShapeRegularPentagon
Rectangle: msoShapeRectangle
Rectangle, Rounded: msoShapeRoundedRectangle
Star, 4-point: msoShape4pointStar
Star, 5-point: msoShape5pointStar
Star, 8-point: msoShape8pointStar
Star, 16-point: msoShape16pointStar
Star, 24-point: msoShape24pointStar
Star, 32-point: msoShape32pointStar
Smiley Face: msoShapeSmileyFace
Sun: msoShapeSun
Trapezoid: msoShapeTrapezoid
Triangle, Isosceles: msoShapeIsoscelesTriangle
Triangle, Right: msoShapeRightTriangle

You can find VBA names of many other shapes on this MSDN page.

We tested this code using PowerPoint 2013 for Windows.

You May Also Like: Use VBA in PowerPoint, Even if You Can’t Program for Nuts | Create Spirographs in PowerPoint Programmatically | Resources on PowerPoint Programming and VBA


Steve RindsbergSteve Rindsberg has been associated with PowerPoint since the product originated more than two decades ago — his PowerPoint FAQ site is a treasure trove of PowerPoint information.

When he’s not updating his site, he’s working on his popular PPTools add-ins for PowerPoint. Steve’s also into a lot of print technology related stuff.

See Also: VBA Code: Remove Underlines from Descenders

Manon Mikkers MinningManon Mikkers Minning is a PowerPoint specialist based in the Netherlands. She is associated with Betere Presentaties, a Dutch presentation design agency. Manon also helped with our challenge at Indezine to create the perfect Circle Illusion Animation in PowerPoint.

Filed Under: Programming
Tagged as: , , , ,

2 Comments

Microsoft and the Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.

Plagiarism will be detected by Copyscape

© 2000-2018, Geetesh Bajaj - All rights reserved.

since November 02, 2000