这个问题已经是第二次遇到了,感觉Office很不靠谱,辛辛苦苦写的文档,之前都好好的,然后某一天打开,发现里面所有的表格都没有了边框,好奇怪... 最要命的是这个文档你需要马上提交,这个时候宏就派上用场了。
首先来看一个遍历所有表格的宏:
Sub Hong2()
Dim tb As Table
With ActiveDocument
For Each tb In .Tables
Next
End With
End Sub
很简单吧,下面就不绕圈子了,直接来结果吧:
Sub Hong2()
Dim tb As Table
With ActiveDocument
For Each tb In .Tables
With tb
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders.Shadow = False
End With
Next
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = wdColorAutomatic
End With
End Sub
哈哈,完成了....
把这段代码粘贴到宏编辑器里,然后运行,ok,问题解决了...
根据这个,还可以衍生出其它的一些宏,下面是我写的,以备不时只需:
自动选中所有表格:
Sub SelectAllTables()
Dim mytable As Table
Application.ScreenUpdating = False
For Each mytable In ActiveDocument.Tables
mytable.Range.Editors.Add wdEditorEveryone
Next
ActiveDocument.SelectAllEditableRanges (wdEditorEveryone)
ActiveDocument.DeleteAllEditableRanges (wdEditorEveryone)
Application.ScreenUpdating = True
End Sub
遍历所有的表,依次选中表头:
Sub SelectTableHeader()
Dim tb as Table
With ActiveDocument
For Each tb In .Tables
.Range(tb.Cell(1,1).Range.Start,tb.Cell(1,tb.Columns.Count).Range.End).Select
Next
End With
End Sub
遍历所有的表头,将表头背景设置为浅灰色:
Sub Hong3()
Dim tb as Table
With ActiveDocument
For Each tb In .Tables
.Range(tb.Cell(1,1).Range.Start,tb.Cell(1,tb.Columns.Count).Range.End).Select
Selection.Shading.BackgroundPatternColor = wdColorGray20
Next
End With
End Sub
下面的代码实现同样的功能:
Sub Hong4()
Dim tb As Table
With ActiveDocument
For Each tb In .Tables
.Range(tb.Cell(1, 1).Range.Start, tb.Cell(1, tb.Columns.Count).Range.End).Shading.BackgroundPatternColor = wdColorGray20
Next
End With
End Sub
下面的代码设置表头的字体和颜色:
Sub Hong5()
Dim tb As Table
With ActiveDocument
For Each tb In .Tables
.Range(tb.Cell(1, 1).Range.Start, tb.Cell(1, tb.Columns.Count).Range.End).Font.Color = wdColorRed
.Range(tb.Cell(1, 1).Range.Start, tb.Cell(1, tb.Columns.Count).Range.End).Font.Size = 9
Next
End With
End Sub