Search code examples
vbapowerpoint

How would I modify this program to apply this change to all Powerpoint slides?


I am trying to quickly change all the colors of tables in a powerpoint, without having to manually do it. This powerpoint has 200 slides, and nearly all of them have tables. I have already figured out how to change all the fonts and sizes of the text in the tables using a different script, but I want to change the color itself. Find attached the code that I found on the internet. This code works, but only for the active slide. I was wondering how to modify it to work for all slides at once. I tried to modify it myself but it didn't work.

Public sub TblCellColorFill()

Dim X As Integer
Dim Y As Integer
Dim oTbl as Table

set oTbl = ActiveWindow.Selection.Shaperange(1).Table   'Only works is a single table shape is selected - add some checks in your final code!

    For X = 1 To otbl.Columns.Count

        For Y = 1 To otbl.Rows.Count

            With otbl.Cell(Y, X)

                If .Selected <> False Then  'Strange bug - will ignore if statement entirely if you use "= True"
                    'Debug.Print "Test worked " & Now

                  'We have the shape we need
                    .shape.Fill.ForeColor.RGB = RGB(100, 150, 200) 'Add your color here

                End If
            End With
        Next    'y
    Next    'x
End Sub

As mentioned, this works but I want it to do all the slides in the powerpoint file at once. Is there any way to modify this code so that it works such?

Credit to @GreatSheikhs on this forum for the code.


Solution

  • You can loop through each slide and table on silde like this.

    Sub ChangeTableColor()
      Dim oSlide As Slide
      Dim oShape As Shape
      Dim oTable As Table
      Dim i As Long, j As Long
      For Each oSlide In ActivePresentation.Slides
        For Each oShape In oSlide.Shapes
          If oShape.HasTable Then
            Set oTable = oShape.Table
            For i = 1 To oTable.Rows.Count
              For j = 1 To oTable.Columns.Count
                oTable.Cell(i, j).Shape.Fill.ForeColor.RGB = RGB(100, 150, 200)
              Next j
            Next i
          End If
        Next oShape
      Next oSlide
    End Sub