Search code examples
vbapowerpoint

Trying to change all text tables in PPT slides to text color and cell fill color?


Trying to loop through all slides in a powerpoint and change whatever text tables there are in each slide to a blue color and black text.

getting a compile error here on the .font.color line

Sub TableAllBlueandBlack()

Dim lRow As Integer
Dim lCol As Integer
Dim oTbl As Table
Dim osld As Slide
Dim oShp As Shape

With ActivePresentation
    For Each osld In .Slides
        For Each oShp In oSdl.Shapes
            Set oTbl = oShp.Table
                With oTbl
                    For lRow = 1 To .Rows.Count
                            For lCol = 1 To .Columns.Count
                                With .Cell(lRow, lCol).Shape
                                    .Fill.ForeColor.RGB = RGB(211, 225, 241) 
                                    .Font.Color = RGB(0, 0, 0)
                                End With
                            Next
                    Next
                End With
            Next
        Next
End With
End Sub

any ideas?


Solution

    • oSdl in oSdl.Shapes is a typo, it should be osld (Dim osld As Slide). Using Option Explicit to avoid this kind of problem.
    • Using .TextFrame.TextRange.Font.Color to update font color
    • Check if the shape is a table with oShp.HasTable, otherwise oShp.HasTable may raise error.
    Option Explicit
    Sub TableAllBlueandBlack()
        Dim lRow As Integer
        Dim lCol As Integer
        Dim oTbl As Table
        Dim oSld As Slide
        Dim oShp As Shape
        With ActivePresentation
            For Each oSld In .Slides
                For Each oShp In oSld.Shapes '**
                    If oShp.HasTable Then '**
                        Set oTbl = oShp.Table
                        With oTbl
                            For lRow = 1 To .Rows.Count
                                For lCol = 1 To .Columns.Count
                                    With .Cell(lRow, lCol).Shape
                                        .Fill.ForeColor.RGB = RGB(211, 225, 241)
                                        .TextFrame.TextRange.Font.Color = RGB(0, 0, 0) '**
                                    End With
                                Next
                            Next
                        End With
                    End With
                Next
            Next
        End With
    End Sub