Jason Stallard
Jason Stallard

Reputation: 348

How to amend Powerpoint Chart Title text using VBA

I have a Powerpoint 2013 presentation with several charts on a slide. I want to amend the charts title but can't figure out how to select the text.

Activepresentation.Slides(1).Shapes("Chart 2").Chart.Title = "some text" doesn't change anything. If I prefix that line with Set it gives "Compile error: Invalid use of property"

ActivePresentation.Slides(1).Shapes("Chart 2").TextFrame.TextRange.Text = "some text" gives "Run-time error '-2147024809 #80070057#': The specified value is out of range"

The chart title is two lines with a mixture of text sizes. It entire chart has been copied from Excel. The result I need is to amend the second half of that title programmatically.

In Excel Sheets("Sheet 1").ChartObjects("Chart 2").Name returns the name but Sheets("Sheet 1").ChartObjects("Chart 2").HasTitle gives "Run-time error '438': Object doesn't support this property or method".

Upvotes: 0

Views: 1019

Answers (1)

Jon Peltier
Jon Peltier

Reputation: 6063

Your syntax is incomplete; you need Chart, which you have in one place but not the others, the chart element is ChartTitle not Title, and you need to specify what subelement gets the new text (Text):

ActivePresentation.Slides(1).Shapes("Chart 2").Chart.ChartTitle.Text = "some text"

Between Shapes or ChartObject and any property or method of a chart, you need Chart:

Sheets("Sheet 1").ChartObjects("Chart 2").HasTitle

fails, but

Sheets("Sheet 1").ChartObjects("Chart 2").Chart.HasTitle

returns True or False.

This is harder without a macro recorder in PowerPoint (there was one until Office 2007 removed it), so you need to use Excel's, and then hack away to make it fit into PowerPoint VBA.

Upvotes: 2

Related Questions