Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerPoint VBA Changing font color of selected item in chart

I know this sounds simple, but I haven't found any solution in the web nor this site.

So, I have a macro in Excel that changes the font color of any selected item -- range, chart, textbox, etc. -- and the code is quite simple:

Selection.Font.Color = RGB(0,0,0)

But in PowerPoint there is not such thing as the "Selection" wildcard in Excel. In PowerPoint this work well for textboxes --

ActiveWindow.Selection.TextRange.Font.Color = RGB(0,0,0)

But it does not work for charts and tables. So, for charts I use this code --

ActiveWindow.Selection.ShapeRange(1).Chart.ChartArea.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB RGB(0,0,0)

The problem is that it changes all the text in the chart -- title, labels, axis, etc. And I only need to change the font color of the selected item. for example, only the title or only the labels, depending on what the user decides to select.

I don't know how to discriminate what item is selected in the chart, in order to apply the change only to it. For example --

.ChartTitle.Format.Fill.ForeColor.RGB, etc.

Is there a way to identify what item is selected? Or to apply the changes only to it? Thank you.

like image 349
manu197a Avatar asked Nov 26 '25 18:11

manu197a


2 Answers

The simple answer to your question is, unfortunately, no. The PPT object model has no way of returning the selected item in a chart.

John Korchok's suggestion should allow you to offer the desired colors to your users to choose, though.

like image 133
Steve Rindsberg Avatar answered Nov 29 '25 14:11

Steve Rindsberg


You were very close. Instead of .ChartArea you can access other chart items, such as .ChartTitle.

This example loops through all the shapes on a slide, including some extra checks to prevent errors. If the shape has a chart, the chart has a title and title has text, specify the color for the chart's title font.

Sub FontColor_ChartTitle()

    With ActivePresentation.Slides(1)

        'Loop through all shapes on the slide
        For i = 1 To .Shapes.Count

            With .Shapes(i)

                'If the shape is a chart
                If .HasChart Then

                    'If the chart has a title
                    If .Chart.HasTitle Then

                        With .Chart.ChartTitle.Format.TextFrame2

                            'If the title contains text
                            If .HasText Then

                                With .TextRange.Font

                                    .Fill.ForeColor.RGB = RGB(95, 37, 97)

                                End With

                            End If

                        End With

                    End If

                End If

            End With

        Next

    End With

End Sub
like image 24
Jenn Avatar answered Nov 29 '25 16:11

Jenn