Supercharge your PowerPoint productivity with

Supercharge your PPT Productivity with PPTools - Click here to learn more.

Tell me about PPTools

Working with PowerPoint tables


PPTools
Shape Styles brings the power of styles to PowerPoint. Apply complex formatting with a single click
Merge Excel, CSV or tab-delimited data into PowerPoint presentations to create certificates, awards presentations, personalized presentations and more
FixLinks prevents broken links when you distribute PowerPoint presentations
Optimizer saves disk space and bandwidth, shrinks your PowerPoint presentations to the right size for email, screenshow or printing
PPT2HTML gives you full control of PowerPoint HTML output, helps meet Section 508 accessibility requirements
Prep4PDF preserves interactivity in PowerPoint presentations when you convert to PDF
Image Export converts PowerPoint slides to JPG, PNG, GIF, WMF and more

How to change text, fill and other table properties

While you can set a PowerPoint table's position and size, you can't directly do much else with it. To get at the contents, you need to understand that PowerPoint treats a table as a kind of array of "cells".

To work with the cell in row three, column 2, for example, you'd address the table's .Cell(3,2) for example.

Each Cell has various properties; the one we're interested in is .Shape. A cell's .Shape property returns a PowerPoint shape that has all the properties of a normal PowerPoint rectangle.

So to change the text in our row three, column two cell we could do this if the table's currently selected:

With ActiveWindow.Selection.ShapeRange(1).Table
    With .Cell(3,2).Shape
        With .TextFrame.TextRange
            .Text = "You found me!"
        End With
    End With
End With

Here's an example that demonstrates how to get at all the text in a table and how to change the color of cells:

Sub FunWithShapesInTable()

    Dim oTbl As Table
    Dim lRow As Long
    Dim lCol As Long

    ' Get a reference to a table either programmatically or
    ' for demonstration purposes, by referencing the currently
    ' selected table:
    Set oTbl = ActiveWindow.Selection.ShapeRange(1).Table
    With oTbl

        For lRow = 1 To .Rows.Count
            For lCol = 1 To .Columns.Count
                With .Cell(lRow, lCol).Shape

                    ' Do something with each cell's text
                    If .HasTextFrame Then
                        If .TextFrame.HasText Then
                            Debug.Print .TextFrame.TextRange.Text
                        End If
                    End If

                    ' do something with each cell ... set the fill:
                    .Fill.Visible = msoTrue
                    .Fill.ForeColor.RGB = RGB(255, 0, 0)

                End With
            Next    ' column
        Next    ' row

    End With

    Set oTbl = Nothing

End Sub

Another set of examples that goes into more detail, allowing you to set cell border visibility, color, weight, to retrieve text from the current cell the cursor's in, to determine which cell the cursor's in and more:

Sub TableExamplesOne()

    Dim oSh As Shape
    Dim oTbl As Table
    Dim lRowCount As Long
    Dim lColumnCount As Long
    Dim lBorderItem As Long

    ' This assumes that there's a table on the current slide
    ' and that the table or something in it is selected

    Set oSh = ActiveWindow.Selection.ShapeRange(1)
    With oSh
        If .HasTable Then
            Set oTbl = oSh.Table
            With oTbl
                Debug.Print .Rows.Count
                Debug.Print .Columns.Count

                ' insert a new row (seems you can't insert one at end of table)
                .Rows.Add (.Rows.Count)

                ' Change the first row cells
                ' Make them hideous so you can't possibly miss the
                ' results of running the macro ;-)
                For lColumnCount = 1 To .Columns.Count

                    With .Cell(1, lColumnCount)

                        ' set all cell borders to invisible
                        ' as a precaution but color/weight them
                        For lBorderItem = 1 To 6
                            With .Borders(lBorderItem)
                                .Visible = msoFalse
                                .ForeColor.RGB = RGB(0, 255, 0)
                                .Weight = 6
                            End With
                        Next

                        ' then set the ones we want visible
                        .Borders(ppBorderBottom).Visible = msoTrue
                        .Borders(ppBorderTop).Visible = msoTrue
                        .Borders(ppBorderLeft).Visible = msoTrue
                        .Borders(ppBorderRight).Visible = msoTrue


                        With .Shape

                            ' fill with red
                            .Fill.Visible = msoTrue
                            .Fill.ForeColor.RGB = RGB(255, 0, 0)

                            ' bold the text
                            .TextFrame.TextRange.Font.Bold = msoTrue

                        End With    ' Shape
                    End With    ' cell
                Next    ' cell

            End With
        End If
    End With
End Sub

Sub TableExamplesTwo()
' can we work out the table or cell from text selected
' within a cell?
'
' This assumes that there's already a table on the slide and that
' text in the table is selected or that the insertion cursor
' is in a cell

    Dim oSh As Shape
    Dim oTbl As Table
    Dim lRowCount As Long
    Dim lColumnCount As Long

    With ActiveWindow.Selection.TextRange
        ' note that you can work out an object's "ancestry" by
        ' walking up its parental chain.  TypeName tells you
        ' what type of object is found at each level:
        Debug.Print "========  Family Tree ========"
        Debug.Print TypeName(.Parent)
        Debug.Print TypeName(.Parent.Parent)
        Debug.Print TypeName(.Parent.Parent.Parent)

        ' that lets us walk back up the tree to the shape
        ' that contains the selected text
        Set oSh = .Parent.Parent
        With oSh
            Debug.Print "======== Current cell's text ========"
            Debug.Print .TextFrame.TextRange.Text
        End With

        ' but which cell is this?
        ' the currently selected SHAPE is the parent table
        Set oTbl = ActiveWindow.Selection.ShapeRange(1).Table
        With oTbl
            For lRowCount = 1 To .Rows.Count
                For lColumnCount = 1 To .Columns.Count
                    If .Cell(lRowCount, lColumnCount).Shape.Name = oSh.Name Then
                        ' FOUND IT
                        Debug.Print "======== Current cell coordinates ========"
                        Debug.Print "Cursor is in cell at row: " & CStr(lRowCount) _
                            & " , column: " & CStr(lColumnCount)
                    End If
                Next
            Next
        End With

    End With

End Sub


Page copy protected against web site content infringement by Copyscape Contents © 1995 - 2008 Stephen Rindsberg, Rindsberg Photography, Inc. and members of the MS PowerPoint MVP team. You may link to this page but any form of unauthorized reproduction of this page's contents is expressly forbidden.

Español    Deutsch    Français    Português    Italiano    Nederlands    Greek    Japanese    Korean    Chinese



Supercharge your PPT Productivity with PPTools


content authoring & site maintenance by
Friday, the automatic faq maker (logo)
Friday - The Automatic FAQ Maker

Working with PowerPoint tables
http://www.pptfaq.com/FAQ00790.htm
Last update 04 April, 2007