Word VBA技术:对文档中的所有表格应用统一的边框

您所在的位置:网站首页 表格边框设置为所有框线 Word VBA技术:对文档中的所有表格应用统一的边框

Word VBA技术:对文档中的所有表格应用统一的边框

2024-06-12 18:39| 来源: 网络整理| 查看: 265

标签:Word VBA

如果你的文档中或者他人传给你的文档中有很多表格,但这些表格有不同样式的边框,而你现在想将这些表格的边框设置为统一的样式,怎么办?当然,你可以逐个表格逐个表格地设置,但是如果文档中的表格很多,这样的操作既繁琐又浪费时间。这种情形下,VBA就派上用场了。

下面的代码为当前文档中的所有表格设置统一的边框样式:

代码语言:javascript复制Sub AllTablesSetUniformBorders() Dim strTitle As String Dim strMsg As String Dim Style As VbMsgBoxStyle Dim Response As VbMsgBoxResult Dim objTable As Table Dim objBorderStyle As WdLineStyle Dim objBorderWidth As WdLineWidth Dim objBorderColor As WdColor Dim objArray As Variant Dim n As Long Dim i As Long '可以将下面的值修改为想要的样式,线宽和颜色 objBorderStyle = wdLineStyleSingle objBorderWidth = wdLineWidth075pt objBorderColor = wdColorBlack strTitle = "给文档中所有表格设置统一的边框" '如果当前文档中包含有表格 If ActiveDocument.Tables.Count > 0 Then strMsg = "给当前文档所有表格设置统一边框." & vbCr & vbCr & "想继续吗?" Style = vbYesNo + vbQuestion Response = MsgBox(strMsg, Style,strTitle) If Response vbYes Then Exit Sub Else '当前文档中没有找到表格 MsgBox "文档中没有表格.",vbInformation, strTitle Exit Sub End If '使用要更改的边框定义数组 '这里不包括对角线边框 objArray = Array(wdBorderTop, _ wdBorderLeft, _ wdBorderBottom, _ wdBorderRight, _ wdBorderHorizontal, _ wdBorderVertical) For Each objTable In ActiveDocument.Tables '统计表格数量 n = n + 1 With objTable For i = LBound(objArray) To UBound(objArray) '如果仅1行且wdBorderHorizontal If .Rows.Count = 1 And objArray(i) = wdBorderHorizontal Then GoTo Skip '如果仅1列且wdBorderVertical If .Columns.Count = 1 And objArray(i) = wdBorderVertical Then GoTo Skip With .Borders(objArray(i)) .LineStyle = objBorderStyle .LineWidth = objBorderWidth .Color = objBorderColor End With Skip: Next i End With Next objTable MsgBox "完成对" & n & "个表格设置边框.",vbOKOnly, strTitle End Sub

你可以修改代码,使得表格边框的样式是你想要的。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3