Changing Color Formatting in Shapes in Office 2010

Office Quick Note banner

Programmatically Working with Shapes in Office 2010: Learn how to manipulate the color format property of a shape in a Microsoft PowerPoint 2010 presentation.

Applies to: Excel 2010 | Office 2010 | PowerPoint 2010 | VBA | 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:   April 2011

Provided by:    Frank Rice, Microsoft Corporation

Microsoft Office 2010 includes the ability to work with many different types of art shapes. Some examples of shapes include lines, connectors, basic shapes, flowchart elements, stars and banners, and callouts. You can enhance and manipulate the appearance of shapes programmatically with a variety of classes and object. In this topic, you programmatically create a simple shape and then manipulate it by changing its brightness property values. To complete this task, you must do the following:

  • Add a Standard Module to a PowerPoint Presentation

  • Add the Code to the Visual Basic Editor

  • Test the Solution

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

  1. Start PowerPoint 2010.

  2. On the Developer tab, click Visual Basic. This opens the Visual Basic Editor.

    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.

Add the Code to the Visual Basic Editor

In this task, you add programming code to the Visual Basic Editor. The code selects the first slide, adds a shape to it, and then changes the brightness of the shape's foreground.

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 TestBrightness()
        Dim i As Integer
        Dim shp As Shape
        Dim sld As Slide
    
        Set sld = ActivePresentation.Slides(1)
    
        ' Add a new shape: A 200x100 pixel balloon, and set its color:
        Set shp = sld.Shapes.AddShape(msoShapeBalloon, 10, 10, 200, 100)
        shp.Fill.ForeColor.RGB = 3487637
    
        ' Finally, alter the Brightness of the color. Do not use
        ' this technique to create animations--PowerPoint handles
        ' that itself. This is meant only as instructive code that
        ' demonstrates how modifying the Brightness property
        ' changes the way a shape looks.
        For i = 0 To 100
            SetBrightness shp, i / 100
            ' Wait 1/10 second or so.
            Pause 0.1
        Next i
    End Sub
    
    Sub SetBrightness(shp As Shape, brightnessValue As Single)
        ' Set the Brightness property of a ColorFormat object.
        ' You can retrieve a ColorFormat in a number of ways.
        ' See this page for more information on ways to retrieve
        ' a reference to a ColorFormat object:
        ' https://msdn.microsoft.com/en-us/library/ff744611.aspx
    
        Dim cf As ColorFormat
        Set cf = shp.Fill.ForeColor
        cf.brightness = brightnessValue
    End Sub
    
    Function Pause(numberOfSeconds As Variant)
        Dim startTime, endTime As Variant
    
        startTime = Timer
        endTime = startTime + numberOfSeconds
    
        Do While Timer < endTime
            DoEvents
        Loop
    End Function
    

Test the Solution

In this task, you step through the code. The best way to see the code in action is to place the Visual Basic Editor window and the PowerPoint window side-by-side.

To step the code

  1. Drag the Visual Basic Editor to the right side of the screen.

  2. Next, drag the PowerPoint window to the left side of the screen and adjust both windows until you can see both.

  3. Place your cursor in the TestBrightness module and then press F8 to step through the code line-by-line and watch the shape being created and its brightness modified.

Next Steps