Modifying Chart Titles in Office 2010
Programmatically Working with Office 2010 Charts: Learn how to update a Microsoft PowerPoint 2010 chart by changing various settings of the title.
Applies to: Microsoft PowerPoint 2010
Published: March 2011
Provided by: Frank Rice, Microsoft Corporation
Microsoft Office 2010 includes a powerful and versatile charting engine. You can chose from many common business and technical chart types. You can also enhance and manipulate the appearance of your charts programmatically by using various classes and objects. In this topic, you programmatically create a simple chart and then update it by changing the property values of the title. To complete this task, you must do the following:
Add a Standard Module to a PowerPoint Presentation
In this task, you open a PowerPoint 2010 presentation, open the Visual Basic Editor, and then insert a standard module.
To add a standard module to a PowerPoint presentation
Start PowerPoint 2010.
On the Developer tab, click Visual Basic to open the Visual Basic Editor.
If you do not see the Developer tab in PowerPoint 2010, click the File tab, and then click Options. In the categories pane, click Popular, select Show Developer tab in the Ribbon, and then click OK.
On the Insert menu, click Module. This adds Module1 to the Projects pane on the left side of the Visual Basic Editor.
Add the Code to the Visual Basic Editor
In this task, you add programming code that creates the simple chart and then sets various properties of the chart title.
To add code to the Visual Basic Editor
In the Projects pane, click Module1.
Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.
Sub ModifyChartTitle() Dim shp As Shape Set shp = ActivePresentation.Slides(1).Shapes.AddChart(xlBarClustered) Dim cht As Chart ' You can check the shp.HasChart property to determine if ' the shape has a chart before continuing, but you can ' be sure this particular shape has a chart because you ' just created it. Set cht = shp.Chart ' Add a title. cht.HasTitle = True With cht.ChartTitle .Text = "Sample Chart" ' Set the orientation of the title. ' Horizontal is the default, but you can change it ' to something else. .Orientation = XlOrientation.xlHorizontal ' You can format individual characters, or ' the entire title: With .Characters(1, 1).Font .Size = 24 .Color = xlRed End With .Characters(2).Font.Size = 14 With .Format ' You can modify many other options. ' See the ChartFormat class for more information. .Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent1 With .Shadow .ForeColor.ObjectThemeColor = msoThemeColorAccent4 .Style = msoShadowStyleOuterShadow .OffsetX = 4 .OffsetY = 4 End With End With ' Make sure and leave room for the title in the chart. .IncludeInLayout = True End With End Sub
The data for the chart is contained in a Microsoft Excel 2010 workbook. To create the workbook in code, you must add a reference to the Microsoft Excel 14.0 Object Library. On the Visual Basic Editor menu, click Tools, and then click References.
In the References dialog box, select Microsoft Excel 14.0 Object Library, and then click OK.
Close the Visual Basic Editor.
Test the Solution
In this task, you run the VBA code that creates the chart and modifies properties of the title. The code also opens an Excel 2010 workbook that contains the chart data. To see the code in action, you step through it line-by-line.
To run the code and create the chart
On the Developers tab, click Macros, highlight ModifyChartTitle, and then click Run. Examine the new chart; it is similar to the one in Figure 1.
Figure 1. Running the code creates a new chart and updates the title
The best way to see the code in action is to place the Visual Basic Editor window next to the PowerPoint window and then single-step through each line of code.
To step through the code
Open a new PowerPoint 2010 presentation.
Open the Visual Basic Editor, insert a module, and then paste the code described previously into the module.
Add a reference to the Microsoft Excel 14.0 Object Library to the project.
Add a breakpoint to the code. To add the breakpoint, right-click the following line of code, select Toggle, and then click Breakpoint.
Drag the Visual Basic Editor window to the right side of your monitor.
Drag the PowerPoint window to the left side of your monitor and adjust both windows until you can see them both.
Click the Visual Basic Editor window and then press the F5 key to run the code. The code starts and then pauses at the breakpoint.
Press F8 to step through the code line-by-line; watch the code create the chart and modify the title in the PowerPoint window.