Search code examples
vbapowerpoint

Applying a specific table format to all tables in a Powerpoint presentation (Border, size, multi-colored cells)


I have asked previously how I would modify some code to change the color of tables, which worked wonderfully. But now I am told I need to follow a specific style, which includes multiple colors, a specific border, and for the borders to positioned in a specific way. I can attach an image of what was requested from me.

I tried modifying the code that someone helped me with on this forum previously, but I can't seem to find out how to apply different colors to each column, or to set border color and thickness. Here's what I've come up with, I managed to figure out how to set the column width, but I didn't even know where to start with the different colored cells, and the border function mostly works but doesn't change the leftmost border of the table. but doesn't change the leftmost border of the table.

Sub ChangeTableColor()
  Dim oSlide As Slide
  Dim oShape As Shape
  Dim oBorder As Borders
  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, 100, 100) 'Not sure how to make this line do different colors for different cells.
            oTable.Columns(1).Width = (1.9 * 72)
            oTable.Cell(i, j).Borders(ppBorderBottom).ForeColor.RGB = RGB(255, 255, 255)
            oTable.Cell(i, j).Borders(ppBorderTop).ForeColor.RGB = RGB(255, 255, 255)
            oTable.Cell(i, j).Borders(ppBordeLeft).ForeColor.RGB = RGB(255, 255, 255)
            oTable.Cell(i, j).Borders(ppBorderRight).ForeColor.RGB = RGB(255, 255, 255)
          Next j
        Next i
      End If
    Next oShape
  Next oSlide
End Sub

I apologize if this is a silly question, to be quite honest I have nearly no experience coding and I'm just trying to automate something for my work. Any help is appreciated!

Thanks to user @taller_ExcelHome on this forum for help with the base code.


Solution

  • The color of the table cells can be set using Shape.Fill.ForeColor.RGB. The weight of the table borders can be accessed through Borders(..).Weight. Note: The provided code may need to be adapted if the tables have more than 4 rows.

    Sub ChangeTableColor()
        Dim oSlide As Slide
        Dim oShape As Shape
        Dim oBorder As Borders
        Dim oTable As Table
        Dim i As Long, j As Long
        Dim aColor, bColor As Long, iBorder As Integer
        aColor = Array(&HB8DFCA, &HD0F2FD, &HDBF0E5, &HF6EBE0)
        bColor = RGB(255, 255, 255)
        For Each oSlide In ActivePresentation.Slides
            For Each oShape In oSlide.Shapes
                If oShape.HasTable Then
                    Set oTable = oShape.Table
                    oTable.Columns(1).Width = (1.9 * 72)
                    For i = 1 To oTable.Rows.Count
                        For j = 1 To oTable.Columns.Count
                            If i < 4 Then ' if all tables have no more than four rows
                                oTable.Cell(i, j).Shape.Fill.ForeColor.RGB = aColor(i - 1)
                            End If
                            For iBorder = 1 To 4
                                With oTable.Cell(i, j).Borders(iBorder)
                                    .ForeColor.RGB = bColor
                                    .Weight = 5
                                End With
                            Next iBorder
                        Next j
                    Next i
                End If
            Next oShape
        Next oSlide
    End Sub
    
    

    enter image description here