Neil Caffrey
Neil Caffrey

Reputation: 31

Shape names PPT VBA

I am trying to reference shapes by index in PPT VBA. It is working but it is a hit and miss because I am cutting and pasting them to different slides so the indexes keep changing. Is there another way to reference shapes so I don't have to deal with the indexes? Here is an example of my code:

ActivePresentation.Slides(1).Shapes(3).Cut
ActivePresentation.Slides(2).Shapes.Paste
ActivePresentation.Slides(1).Shapes(4).Cut
ActivePresentation.Slides(2).Shapes.Paste

 With ActivePresentation.Slides(1).Shapes(3)
.Height = 325
.Width = 325
.Left = 190
.Top = 90

End With

With ActivePresentation.Slides(1).Shapes(4)
.Height = 600
.Width = 600
.Left = 65
.Top = 360

End With

Upvotes: 1

Views: 1155

Answers (1)

Steve Rindsberg
Steve Rindsberg

Reputation: 14809

Tag the shape before cutting, then call a function to return the shape tagged with that value from the target slide after pasting:

With ActivePresentation.Slides(1).Shapes(3)
  .Tags.Add "SomeName", "SomeValue" 
  ' whatever name and value you like
  .Cut
End With

' Then paste it onto another slide
' and to get a reference to it:

Dim oSh as shape
Set oSh = ShapeTaggedWith(oSl, "SomeName", "SomeValue")
If Not oSh is Nothing Then

End If
' etc

Function ShapeTaggedWith(oSl as Slide, sTagName as String sValue as String) as Shape
   Dim oSh as Shape
   For each oSh in oSl.Shapes
      If oSh.Tags(sTagName) = sValue Then
          Set ShapeTaggedWith = oSh
          Exit Function
      End If
   Next

End Function

Upvotes: 1

Related Questions