This documentation is archived and is not being maintained.

Modifying Chart Legends in Office 2010

Office 2010

Office Quick Note banner

Programmatically Working with Office 2010 Charts: Learn how to update a Microsoft PowerPoint 2010 chart by changing various settings of the legend.

Last modified: April 22, 2011

Applies to: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010

In this article
Add a Standard Module to a PowerPoint Presentation
Add the Code to the Visual Basic Editor
Test the Solution
Next Steps

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:

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

  1. Start PowerPoint 2010.

  2. On the Developer tab, click Visual Basic to open the Visual Basic Editor.

    Note Note

    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.

  3. On the Insert menu, click Module. This adds Module1 to the Projects pane on the left side of 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

  1. In the Projects pane, click Module1.

  2. 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
  3. 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.

  4. In the References dialog box, select Microsoft Excel 14.0 Object Library, and then click OK.

  5. Close the Visual Basic Editor.

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

    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

  1. Open a new PowerPoint 2010 presentation.

  2. Open the Visual Basic Editor, insert a module, and then paste the code described previously into the module.

  3. Add a reference to the Microsoft Excel 14.0 Object Library to the project.

  4. Add a breakpoint to the code. To add the breakpoint, right-click the following line of code, select Toggle, and then click Breakpoint.

    With cht.Legend

  5. Drag the Visual Basic Editor window to the right side of your monitor.

  6. Drag the PowerPoint window to the left side of your monitor and adjust both windows until you can see them both.

  7. 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.

  8. Press F8 to step through the code line-by-line; watch the code create the chart and modify the legend in the PowerPoint window.