The Name property of PowerPoint shapes is not reliable
Using the .Name property to get a reference to a shape on a slide, master, layout, notes page, etc. isn't entirely reliable and can lead to very odd results.
- Start a new presentation.
- Add a bit of text to the Title placeholder of the title slide that PowerPoint adds automatically.
- Rightclick the border of the Title placeholder and drag to make a copy of it.
- Repeat several times if you like.
- Now check the name of each of these shapes. For example, select each one in turn then:
They're all named "Title 1".
PowerPoint seems to give you a reference to the first shape by that name if you use code like
Different versions of PowerPoint may behave slightly differently, but on current versions it produces identically named shapes when you
- Copy placeholder shapes
- Copy shapes whose name has been changed from the name PowerPoint initially gave it
If you insert a shape and don't change the name PowerPoint assigns it, PowerPoint will give it a new name when you copy the shape.
Rather than using a shape's .Name property, use its .ID. This function will return the shape with any given ID from a slide:
Function ShapeWithID(oSl As Slide, lID As Long) As Shape ' Returns a reference to the shape with ID lID ' on slide oSl (or Nothing) Dim oSh As Shape For Each oSh In oSl.Shapes Debug.Print oSh.Id If oSh.Id = lID Then Set ShapeWithID = oSh Exit Function End If Next End Function
Use this to test the function:
Sub TestShapeWithID() Dim oSh As Shape ' Change the slide number and ID as needed: Set oSh = ShapeWithID(ActivePresentation.Slides(1), 9) If Not oSh Is Nothing Then oSh.Select End If End Sub
Another approach is to use .Tags to identify slides or shapes that must be retrieved later. Learn more about using Tags in PowerPoint here: