Change the colors of all borders in all tables in the word vba

I am trying to write a macro to immediately change the colors of all the borders of all tables in a Word document.

My attempt to do this only changes the upper and lower bounds in my tables:

Sub ChangeTableBordersColors()
Dim mycolor As WdColor
Dim myTable As Table
mycolor = wdColorRed
For Each myTable In ActiveDocument.Tables
With myTable
.Borders(wdBorderTop).Color = mycolor
.Borders(wdBorderBottom).Color = mycolor
.Borders(wdBorderHorizontal).Color = mycolor
.Borders(wdBorderVertical).Color = mycolor
.Borders(wdBorderLeft).Color = mycolor
.Borders(wdBorderRight).Color = mycolor
End With
Next
End Sub

Can someone tell me how to fix this?

+4
source share
2 answers

I did not get the full mechanics, but it seems that your code does not work when some boundaries are set individually. Resetting LineStyle and LineWidth first, then color, works for me.
You can shorten this to:

    With myTable
        .Borders.InsideLineStyle = wdLineStyleSingle
        .Borders.InsideLineWidth = wdLineWidth025pt
        .Borders.InsideColor = mycolor

        .Borders.OutsideLineStyle = wdLineStyleSingle
        .Borders.OutsideLineWidth = wdLineWidth025pt
        .Borders.OutsideColor = mycolor
    End With
+1
source

KekuSemau - , , . , , . .

( !):

Sub ChangeTableBordersColors()
Dim mycolor As WdColor
Dim aTable As Table
Dim i As Integer
Dim j As Integer
Dim Row As Integer
Dim Column As Integer

mycolor = wdColorRed
For Each aTable In ActiveDocument.Tables
i = aTable.Rows.Count
j = aTable.Columns.Count
For Row = 1 To i
For Column = 1 To j
    With aTable.Cell(Row, Column)
    .Borders(wdBorderTop).Color = mycolor
    .Borders(wdBorderBottom).Color = mycolor
    .Borders(wdBorderLeft).Color = mycolor
    .Borders(wdBorderRight).Color = mycolor
    End With
Next Column
Next Row
Next
End Sub
+1

Source: https://habr.com/ru/post/1659561/


All Articles