alt
+F11
,插入模块,粘贴以下代码
Sub FormatTable()
Dim tbl As Table
Dim row As row
Dim cell As cell
Dim i As Integer
' 检查是否选中了表格
If Selection.Tables.Count = 0 Then
MsgBox "请先选中一个表格。", vbExclamation
Exit Sub
End If
Set tbl = Selection.Tables(1)
' 去掉表格的两边竖框线和内部竖框线
tbl.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
tbl.Borders(wdBorderRight).LineStyle = wdLineStyleNone
tbl.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
' 将表格居中
tbl.Rows.Alignment = wdAlignRowCenter
For i = 1 To tbl.Rows.Count
If i = 1 Then
' 第一行的顶部和底部框线
tbl.Rows(i).Borders(wdBorderTop).LineStyle = wdLineStyleSingle
tbl.Rows(i).Borders(wdBorderTop).LineWidth = wdLineWidth300pt
' 将第一行内容加粗
tbl.Rows(i).Range.Font.Bold = True
ElseIf i = 2 Then
' 第二行的顶部框线
tbl.Rows(i).Borders(wdBorderTop).LineStyle = wdLineStyleSingle
tbl.Rows(i).Borders(wdBorderTop).LineWidth = wdLineWidth100pt
ElseIf i = tbl.Rows.Count Then
' 最后一行的底部框线
tbl.Rows(i).Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
tbl.Rows(i).Borders(wdBorderBottom).LineWidth = wdLineWidth300pt
Else
' 其他行的框线全部去掉
tbl.Rows(i).Borders(wdBorderTop).LineStyle = wdLineStyleNone
tbl.Rows(i).Borders(wdBorderBottom).LineStyle = wdLineStyleNone
End If
' 设置单元格高度为0.75厘米
tbl.Rows(i).Height = CentimetersToPoints(0.75)
Next i
' 将列宽自适应每列最大的列内字符长度
tbl.AutoFitBehavior (wdAutoFitContent)
MsgBox "表格格式化完成。", vbInformation
End Sub
操作:选中表格,按alt
+F8
,执行宏