Reputation: 51
I'm trying to write a macro to change the colours of all the borders of all the tables in a word document at once.
My attempt at doing this changes only the top and bottom borders 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 anyone suggest how to fix this?
Upvotes: 4
Views: 3727
Reputation: 51
Thank you so much KekuSemau - once you pointed out what the problem was I could see the solution. My mistake was to try to deal with the whole table when it had individually set borders. I needed to deal with each cell individually.
The following modification worked (although a bit ugly and horribly slow!):
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
Upvotes: 1
Reputation: 6856
I did not get the full mechanics, but is seems that your code does not work when some borders have been set individually.
Resetting the LineStyle and LineWidth first, then the color, works for me.
You can shorten this down to this:
With myTable
.Borders.InsideLineStyle = wdLineStyleSingle
.Borders.InsideLineWidth = wdLineWidth025pt
.Borders.InsideColor = mycolor
.Borders.OutsideLineStyle = wdLineStyleSingle
.Borders.OutsideLineWidth = wdLineWidth025pt
.Borders.OutsideColor = mycolor
End With
Upvotes: 2