7
votes

I am trying to change the text color of the chart title of a histogram chart in PowerPoint.
Here is what I do:

var colorFormat = chart.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor;
colorFormat.RGB = ...;
// or
colorFormat.ObjectThemeColor = ...;

This works for the standard charts like line charts. But it doesn't work for other chart types like histogram, waterfall, tree map etc.

In these cases, setting ObjectThemeColor sets the text to black. Setting RGB does actually set the correct color. However, in both cases, as soon as the user changes the selection, the text color jumps back to the one it had previously.

How can I set the text color of the title of one of these charts?
I am using VSTO and C# but a VBA solution is just as welcome as long as it can be translated to C# and still work.

3
When I specify the exact chart object (for example, Slides[1].Shapes[2].Chart) I am able to apply the color to the chart title without losing the change. Are you using the Selection object to identify the chart instead of other means such as the Shape.Name property? That might explain the loss of the color change when the user changes the selection.joeschwa
@joeschwa: Even if I identify the chart not from a selection, the result is the same. I don't know why it works for you - maybe you are using a chart type that works anyway?Daniel Hilgarth
I am using the histogram chart type in PowerPoint 2016. Are you using a system-defined color when assigning the RGB value? (Something like colorFormat.RGB = Color.DarkOliveGreen.ToArgb()) It may be possible that VSTO requires this.joeschwa
It makes absolutely no difference. ActivePresentation.Slides(19).Shapes(2).Chart.ChartTitle.Characters.Font.Color = vbGreen has the exact same result: 1. It works only if the chart title is selected 2. It reverts back once something else gets selected.Daniel Hilgarth
@DavidZemens: It's not a problem that it only works if the title is selected, as I am working off of the selection anyway. However, the problem is that the change will be undone as soon as the selection is moved to another object.Daniel Hilgarth

3 Answers

0
votes

Based on what info you gave I built a histogram and waterfall chart in PowerPoint and was successful using:

Sub ChartTitleFontColor()
  Dim oShp As Shape
  Dim oCht As Chart

  'Waterfall on slide 1
  Set oShp = ActivePresentation.Slides(1).Shapes(1)
  If oShp.HasChart Then
    Set oCht = oShp.Chart
  End If

  ' Do stuff with your chart
  If oCht.HasTitle Then
    Debug.Print oCht.ChartTitle.Text
    oCht.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(251, 5, 40)
  End If

  'Histogram on slide 2
  Set oShp = ActivePresentation.Slides(2).Shapes(1)
  If oShp.HasChart Then
    Set oCht = oShp.Chart
  End If

  ' Do stuff with your chart
  If oCht.HasTitle Then Debug.Print oCht.ChartTitle.Text
    oCht.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(251, 5, 40)
  End If

  ' Clean up
  Set oShp = Nothing
  Set oCht = Nothing
End Sub

enter image description here

0
votes

Your code works in my test. I created two charts in PowerPoint 2016, the first one a waterfall, and the second another type. The following code changes the title color only (and text just a proof of it being changed) and nothing else. I can select the other chart and nothing changes. I could not find a bug about this in a search. Perhaps something in the remaining code is changing it back?

Sub test()
    Dim myPresentation As Presentation
    Set myPresentation = ActivePresentation

    Dim myShape As Shape
    Set myShape = myPresentation.Slides(1).Shapes(1)

    Dim theChart As Chart
    If myShape.HasChart Then
        Set theChart = myShape.Chart

        If theChart.ChartTitle.Text = "This is blue" Then
            theChart.ChartTitle.Text = "This is yellow"
            theChart.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 255, 0)
        Else
            theChart.ChartTitle.Text = "This is blue"
            theChart.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 255, 255)
        End If
    End If
End Sub
0
votes

This is not exactly an answer but I think you should name your object. Instead of using

ActivePresentation.Slides(1).Shapes(1)

You can name the object. enter image description here