Better way to change multiple cell borders in power point table using VBA? RRS feed

  • Question

  • I'm trying to format borders for all cells of a power point table.   The best I've found is to set borders by row.

    Can anyone suggest a better faster method?



    'As per mentioned on page
    Sub TestTablePerformance2(nrows,ncols)
    Const ppLayout = 2
    Const ppBorderRight = 4
    Const ppBorderBottom = 3
        Set objPPT = CreateObject("PowerPoint.Application")
        Set objPresentation = objPPT.Presentations.Add
        objPPT.Visible = True
        Set objSlide = objPresentation.Slides.Add(1,ppLayout)
        Set sTable = objSlide.Shapes.AddTable(nrows, ncols, 0, 0)
                For row = 1 To sTable.table.Rows.count
          With sTable.table.Rows(row).Cells
                .Borders.Item(ppBorderRight).Weight = 1
                .Borders.Item(ppBorderRight).ForeColor.RGB = RGB(0, 0, 0)
                .Borders.Item(ppBorderBottom).Weight = 1
                .Borders.Item(ppBorderBottom).ForeColor.RGB = RGB(0, 0, 0)
          End With
    End Sub

    Tuesday, December 6, 2016 3:51 PM

All replies

  • Hi Scott Greig,

    when I try to test on my side then I also find that my code is little bit similar with yous.

    Sub Tableformatting()
    Dim r As Long, c As Long
    Dim t As Table
    Set t = ActiveWindow.Selection.ShapeRange.Table
    For r = 1 To t.Rows.Count
        For c = 1 To t.Columns.Count
            With t.Cell(r, c)
                .Borders(ppBorderTop).DashStyle = msoLineSolid
                .Borders(ppBorderBottom).DashStyle = msoLineSolid
                .Borders(ppBorderLeft).DashStyle = msoLineSolid
                .Borders(ppBorderRight).DashStyle = msoLineSolid
                .Borders(ppBorderTop).ForeColor.RGB = RGB(0, 0, 0)
                .Borders(ppBorderBottom).ForeColor.RGB = RGB(0, 0, 0)
                .Borders(ppBorderLeft).ForeColor.RGB = RGB(0, 0, 0)
                .Borders(ppBorderRight).ForeColor.RGB = RGB(0, 0, 0)
            End With
        Next c
    Next r
    End Sub

    also if we try to see the documentation of border in powerpoint then we find something like mentioned below.

    ActiveWindow.Selection.ShapeRange.Table.Rows(2).Cells.Borders(ppBorderBottom).DashStyle = msoLineDash


    Borders Object (PowerPoint)

    so I think applying the borders using Rows.Cells is a proper way here.

    I did not find any way to apply the border to whole table directly.



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, December 7, 2016 1:26 AM