DrS.
DrS.

Reputation: 67

Access a Shape object via its ID

I have a PowerPoint slide with 5 shapes on it. I would like to do different things with theses shapes in a macro. How can I change one of these shapes by using the shape ID? For example, I have two shapes with a name of "Title 1" but I want to use the one with an ID of 15.

Here is my code:

Sub size_n_spread_v()

    Dim j As Integer
    Dim sld As Slide
    Dim SldId As Long
    gap = std_gap

    SldId = ActiveWindow.View.Slide.SlideIndex
    Set sld = ActivePresentation.Slides(SldId)

    Call SortMultArray

    new_dim = (total_dim - gap * (lngRow - 1)) / lngRow

    'This works but is not specific:
    'sld.Shapes.("Title 1").Height = new_dim 

    'This would hopefully be specific but the syntax does not work Please HELP!
    'sld.Shapes.("Title 1").Id(15).Height = new_dim

End Sub

Does someone know the right syntax to change the shape via ID?

Upvotes: 1

Views: 1619

Answers (2)

Jamie Garroch - MVP
Jamie Garroch - MVP

Reputation: 2979

The function mentioned above is the only way to get a shape by Id. You have to search through the Shapes collection as there is no equivalent ShapeIndex as there is for SlideIndex. The other solution to find a specific shape is to uniquely identify shapes by adding your own Tag but this is a more complex solution.

Upvotes: 1

Bond
Bond

Reputation: 16311

I don't know of a way, but you could write a simple helper function that you could then use throughout your project to make things easier on yourself. Something like this would work:

Public Function GetShapeById(s As Slide, n As String, id As Long) As Shape

    Dim objShape As Shape
    For Each objShape In s.Shapes
        If StrComp(objShape.Name, n, vbTextCompare) = 0 And objShape.Id = id Then
            Set GetShapeById = objShape
            Exit Function
        End If
    Next

End Function

Then you could use it like so:

Sub size_n_spread_v()

    ....

    ' Instead of:
    sld.Shapes.("Title 1").Id(15).Height = new_dim

    ' Use:
    GetShapeById(sld, "Title 1", 15).Height = new_dim

End Sub

Upvotes: 2

Related Questions