Search code examples
vbaloopschartspowerpoint

PowerPoint VBA - loop all slides, all shapes, find chart, set datalabel color to Black


I'm new to PowerPoint VBA so please bear with me.

I would like to:

  1. loop through all the slides on a given pptx,
  2. loop through all the shapes on a given slide,
  3. find chart shapes
  4. loop through the chart's series collection
  5. set the datalabel color property to dark black.

So far I have been able to complete the first 3 tasks but I need help with the last 2. Here is my code:

Sub test()

  Dim slide As Object
  Dim shape As Object
  Dim shapeNames As Object
  Dim chSeries As Series

  i = 0
  For Each slide In ActivePresentation.Slides

      For Each shape In slide.Shapes

          If shape.HasChart Then

              i = i + 1
              Debug.Print "found a chart on slide", i

          End If

      Next

  Next

End Sub

Solution

  • Solved.

    Sub test()
    
        Dim sld As Slide
        Dim shp As Shape
        Dim sr As Series
        Dim chrt As Chart
    
            For Each sld In ActivePresentation.Slides
                For Each shp In sld.Shapes
    
                    If shp.HasChart Then
                        Debug.Print shp.Chart.ChartType
    
                        If shp.Chart.ChartType = 57 Then
    
                            shp.Chart.SeriesCollection(1).DataLabels.Font.Color = RGB(0, 0, 0)
    
                         End If
    
                    End If
    
        Next shp
        Next sld
    
    End Sub
    

    Though I didn't successfully loop over the series in chart but this works.