7

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.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 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 Mar 27 '18 at 15:26
  • @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 Mar 27 '18 at 17:29
  • 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 Mar 27 '18 at 19:19
  • No, I am just using a number. And this usually works, so it shouldn't be a problem. Additionally, with theme colors it is the same problem. If you have a presentation with VBA in it that shows how it is working for you, could you please upload it, so I can see if it works for me? I am using Office 365 - which is a Office 16 just like Office 2016 - but it wouldn't be the first thing that is different between the two – Daniel Hilgarth Mar 27 '18 at 19:25
  • Unfortunately, VSTO, in general, and PowerPoint, in particular, have a lot of quirks. The PowerPoint object model in Office 365 was created in 1997 and is virtually the same today. I suggest testing the RGB assignment with a system-defined color without employing the `Selection` object to identify the chart. PowerPoint may be losing the RGB numerical assignment when the selection is changed. – joeschwa Mar 27 '18 at 20:10
  • 1
    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 Mar 28 '18 at 08:04
  • Very strange. When I test your VBA code it works in a pptm file. `Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes[2].Chart.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = Color.DarkOliveGreen.ToArgb();` also works fine in a C# VSTO solution. The only other thing I can think of at the moment is to step through your code and see if another process based on `Selection` is reversing the formatting when the user changes selection manually. – joeschwa Mar 28 '18 at 12:32
  • No, there is no other process. Really strange. – Daniel Hilgarth Mar 28 '18 at 14:28
  • There are definitely some quirks in PowerPoint which defy the general maxim to "avoid reliance on `Select` method/`Selection` object", especially as pertains to some of the finer points of chart/shape manipulations. If your code works while the title is selected, it may be the only solution is to have your code `Select` that title object. This is of course less-than-optimal for a number of reasons. – David Zemens Mar 30 '18 at 13:36
  • 1
    @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 Mar 30 '18 at 15:48
  • Just spitballing here: are you able to reproduce the error condition manually? can you reproduce the error condition on another machine (whether manually or via the program)? Are you using any sort of template/theme to create the slides & shapes (e.g., a POTX file, etc.)? If you can fix the chart manually, you may be able to solve your problem with a [chart template](https://support.office.com/en-us/article/Save-a-custom-chart-as-a-template-259a5f9d-a9ec-4b3f-94b6-9f5e55187f2a) – David Zemens Mar 30 '18 at 16:40
  • That's the thing: There is no special theme or template. There is no add-in that changes the text color back. And don't forget: The code does work for some chart types! – Daniel Hilgarth Apr 02 '18 at 08:20

3 Answers3

0

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

sunsetsurf
  • 582
  • 4
  • 8
  • Thanks for the effort, but: 1. I am using PowerPoint. 2. It doesn't work for the waterfall chart 3. For the charts for which it works, it changes not only the chart title but also axis titles etc. – Daniel Hilgarth Mar 26 '18 at 09:54
  • I can send you the file I created. The macro changes just the title color and editing the data or other items didn't cause the title color to change back. – sunsetsurf Mar 26 '18 at 23:47
  • 1
    As far as I can tell, you are using PowerPoint 2010 and as far as I know, this version doesn't have a native Waterfall chart type. So, it is working for you, because you are using a normal bar chart that you make look like a waterfall chart. I think the Waterfall chart as a chart type is only available in PP2016 – Daniel Hilgarth Mar 27 '18 at 08:02
0

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
ForEachLoop
  • 2,508
  • 3
  • 18
  • 28
0

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

zawhtut
  • 8,335
  • 5
  • 52
  • 76