Search code examples
vbapowerpoint

Changing font styles for all charts


I'm trying to get my code to change the chart style for all charts. I have managed this for tables, but if I alter the code for charts it doesn't work. Am I missing something? This is the code for tables that works, and it seems to fall over as the Apply Style must use a different style for charts.

Table Code

If oSh.HasTable Then
  Set oTbl = oSh.Table
  oTbl.ApplyStyle ("{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}"), True
End If

I tried the following code

If oSh.HasChart Then
  Set oChr = oSh.Chart
  oChr.ApplyStyle ("{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}"), True
End If

But I get the error

Complile Error :- Method or data member not found.

The table code moves the style to Medium Style 2, Accent 1. I'm expecting charts to have the same effect.


Solution

    • Using ChartSytle to change style

    Microsoft documentation:

    Chart.ChartStyle property (PowerPoint)

    • If the chart is selected,
    Sub ChartSytle()
        Dim shp As Shape
        With ActiveWindow.Selection
            If .ShapeRange.HasChart = msoTrue Then
                Set shp = .ShapeRange(1)
                shp.Chart.ChartStyle = 12  ' 1 to 48, modify as needed
            End If
        End With
    End Sub
    
    • If chart is not selected
    Sub ChartSytle()
        Dim shp As Shape
        Set shp = ActivePresentation.Slides(1).Shapes(1) ' modify as needed
        With shp
            If .HasChart = msoTrue Then
                shp.Chart.ChartStyle = 12  ' 1 to 48, modify as needed
            End If
        End With
    End Sub