Export (0) Print
Expand All

Working with Table Background Formatting in PowerPoint 2010

Office 2010

Office Quick Note banner

Working with Tables in Microsoft Office 2010: Learn how to work with table background colors and textures in Microsoft PowerPoint 2010.

Last modified: May 20, 2011

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:   June 2011

Provided by:    Frank Rice, Microsoft Corporation

Changing the background color and texture, enhances the features and visual display of a table. In this topic, you programmatically add a table to a Microsoft PowerPoint 2010 presentation and then manipulate some of the background properties of the table. 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. This opens 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 to the Visual Basic Editor.

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 WorkWithTableBackground()
        Dim pres As Presentation
        Set pres = ActivePresentation
       
        ' Create a slide with a table on it:
        Dim sld As Slide
        Set sld = pres.Slides.Add(2, ppLayoutTable)
       
        Dim tbl As Table
        Set tbl = sld.Shapes.AddTable(4, 4).Table
        ' Fill the table with data:
        FillTable tbl
             
        ' Retrieve Background property:
        Dim tb As TableBackground
        Set tb = tbl.Background
       
        ' Should be able to do this:
        tb.Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent2
       
        ' But that doesn't work, because when you create a table,
        ' you must also set its fill to None (as you can in the user interface).
        ' There doesn't appear to be any way to do that from VBA. Therefore,
        ' you must rely on a procedure like SetTableColor or SetTableTexture,
        ' which handles each cell individually.
       
        ' This procedure sets the cell color:
        SetTableColor tbl, msoThemeColorAccent2
       
        ' This procedure sets the cell texture:
        SetTableTexture tbl, msoTextureCork
    End Sub
    
    Sub FillTable(tbl As Table)
       
        ' Fill a table with sample data.
        Dim row As Integer
        Dim col As Integer
       
        For col = 1 To tbl.Columns.Count
            tbl.Cell(1, col).Shape.TextFrame.TextRange.Text = "Heading " & col
        Next col
       
        Dim shp As Shape
       
        For row = 2 To tbl.Rows.Count
            For col = 1 To tbl.Columns.Count
                Set shp = tbl.Cell(row, col).Shape
                shp.TextFrame.TextRange.Text = "Cell " & row & ", " & col
                shp.Fill.Visible = msoFalse
            Next col
        Next row
    End Sub
    
    Sub SetTableColor(tbl As Table, themeColor As MsoThemeColorIndex)
        ' Iterate through rows and columns and set the color
        ' for each cell individually. You must use code like this
        ' cause you cannot override the built-in background for a table.
        Dim row As Integer
        Dim col As Integer
       
        For col = 1 To tbl.Columns.Count
            For row = 1 To tbl.Rows.Count
            Dim Fill As Integer
                tbl.Cell(row, col).Shape.Fill.ForeColor.ObjectThemeColor = themeColor
            Next row
        Next col
    End Sub
    
    Sub SetTableTexture(tbl As Table, texture As MsoPresetTexture)
        ' Iterate through rows and columns and set the texture
        ' for each cell individually. You must use code like this
        ' cause you cannot override the built-in background for a table.
        Dim row As Integer
        Dim col As Integer
       
        For col = 1 To tbl.Columns.Count
            For row = 1 To tbl.Rows.Count
            Dim Fill As Integer
                tbl.Cell(row, col).Shape.Fill.PresetTextured texture
            Next row
        Next col
    End Sub
    
    

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

Note Note

Although Microsoft Office PowerPoint 2007 added a TableBackground class to support the Table.Background property, it doesn't allow you to set formatting of the table background, because of limitations of the object model. This example works around that limitation by setting properties for each cell individually. This requires a separate procedure for each type of setting you want to change.

To run the code

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

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

  3. Place your cursor in the WorkWithTableBackground module, press F8 to get started debugging, and then press Shift+F8 to step through the code line-by-line (and bypass external procedures) and watch the code behavior.

Show:
© 2014 Microsoft