Search code examples
vbapowerpointshapes

Is there a way to use a Shape's ID in PowerPoint VBA to then determine it's color?


I have a macro to build a consolidated PowerPoint presentation from about 25 separate, individual slides. The slides have a status circle that are either red, yellow or green to display the current status. I want to pull the color of these shapes and put into an excel file. I have tried using the shape index number to reference the shape in my code, but the index for the particular shape changes on every slide; it is not consistent. The Shape ID seems to be consistent, but I cannot figure out how to use the Shape ID in VBA. This is what I'm currently using:

With ActivePresentation.Slides(IForLoop).Shapes(8).Fill.ForeColor
data = ActivePresentation.Slides(IForLoop).Shapes(8).Fill.Forecolor

If Instr(1 , data, "255"), Then
     LArray (I, sStatus) = "Red"
ElseIf InStr(1, data, "65535") Then
     LArray (I, sStatus) = "Yellow"
ElseIf InStr(1, data, "5287936") Then
     LArray (I, sStatus) = "Green"
End If

The problem is that the Shape reference, Shape(8), is not consistent for each slide so I'm not getting the correct color name to populate in my Excel file.

Thank you.


Solution

  • Untested:

    '...
    Dim shp As Shape
    
    Set shp = getShapeByID(yourIdHere, ActivePresentation.Slides(IForLoop))
    
    If Not shp Is Nothing then
       Select Case shp.Fill.ForeColor
           Case 255: LArray (I, sStatus) = "Red"
           Case 65535: LArray (I, sStatus) = "Yellow"
           Case 5287936: LArray (I, sStatus) = "Green"
       End Select
    End If
    '...
    
    
    'Get a shape from its Id
    Function getShapeByID(shapeID As Long, sl As Slide) As Shape
        Dim s As Shape
        For Each s In sl.Shapes
            If s.id = shapeID Then
                Set getShapeByID = s
                Exit Function
            End If
        Next
    End Function