更改单词vba中所有表中所有边框的颜色

时间:2016-11-01 22:16:38

标签: ms-word word-vba

我正在尝试编写一个宏来同时更改word文档中所有表格的所有边框的颜色。

我尝试这样做只会改变表格中的顶部和底部边框:

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

有人可以建议如何解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

我没有得到完整的机制,但似乎你的代码在单独设置某些边框时不起作用。 首先重置LineStyle和LineWidth,然后重置颜色,对我有效 您可以将此缩短为:

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

        .Borders.OutsideLineStyle = wdLineStyleSingle
        .Borders.OutsideLineWidth = wdLineWidth025pt
        .Borders.OutsideColor = mycolor
    End With

答案 1 :(得分:1)

非常感谢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