manu197a
manu197a

Reputation: 110

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.

Upvotes: 2

Views: 4111

Answers (3)

Steve Rindsberg
Steve Rindsberg

Reputation: 14809

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.

Upvotes: 4

John Korchok
John Korchok

Reputation: 4913

Since the basic issue is providing the client with more colors while avoiding having them input RGB values, I think you might want to evaluate the simplest most flexible way to do that. For color options beyond the theme colors, adding Custom Colors to the file is a better way to go. I would drop all the VBA selection-based formatting. This article emphasizes PowerPoint, but custom colors can be used in Word and Excel as well: Custom Colors added to color picker

Upvotes: 2

Jenn
Jenn

Reputation: 647

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

Upvotes: 2

Related Questions