Modifying Chart Legends in Office 2010
Programmatically Working with Office 2010 Charts: Learn how to update a Microsoft PowerPoint 2010 chart by changing various settings of the legend.
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 with various classes and objects. In this topic, you programmatically create a simple chart and then update it by changing property values of the legend. 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 legend.
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 ModifyChartLegend() Dim shp As Shape Set shp = ActivePresentation.Slides(1).Shapes.AddChart(xl3DPie) Dim cht As Chart Set cht = shp.Chart With cht.Legend .Position = xlLegendPositionTop .IncludeInLayout = True ' The next sections make arbitrary (and perhaps unattractive) ' changes to the formatting of the legend. With .Format With .Fill .ForeColor.ObjectThemeColor = msoThemeColorBackground2 End With With .Line .ForeColor.ObjectThemeColor = msoThemeColorAccent1 .Style = msoLineThinThin .Weight = 2 End With With .Shadow .Type = msoShadow21 .Style = msoShadowStyleOuterShadow .ForeColor.ObjectThemeColor = msoThemeColorAccent4 End With With .Glow .Color.ObjectThemeColor = msoThemeColorAccent2 .Radius = 11 End With End With End With End Sub
The data for the chart is contained in an 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 legend. 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, select ModifyChartLegend, 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 legend
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 legend in the PowerPoint window.