目录
1、表格首行居中
说明: 遇到错误将进行捕获,然后继续处理下一个表格
宏:
java
Sub 表格首行居中()
Dim tbl As table
On Error Resume Next ' 错误时继续执行下一个循环
For Each tbl In ActiveDocument.Tables
On Error Resume Next ' 再次设置错误处理,针对当前表格
With tbl.Rows(1).Range
.ParagraphFormat.Alignment = wdAlignParagraphCenter ' 设置水平居中
End With
With tbl.Rows(1).Range
.Cells.VerticalAlignment = wdCellAlignVerticalCenter ' 设置垂直居中
End With
On Error GoTo 0 ' 重置错误处理,以便在循环的下一次迭代中捕获错误
Next tbl
End Sub
2、表格内容靠左上下居中(排除首行)
说明: 遇到错误将进行捕获,然后继续处理下一个表格
宏:
java
Sub 表格内容靠左上下居中()
Dim tbl As table
Dim row As row
Dim cell As cell
On Error Resume Next ' 设置错误处理,忽略错误继续执行
For Each tbl In ActiveDocument.Tables
' 跳过首行
For Each row In tbl.Rows
If row.Index <> 1 Then ' 首行索引为1,跳过首行
For Each cell In row.Cells
With cell.Range.ParagraphFormat
.Alignment = wdAlignParagraphLeft ' 设置水平靠左
End With
With cell.Range
.Cells.VerticalAlignment = wdCellAlignVerticalCenter ' 设置垂直居中
End With
Next cell
End If
Next row
Next tbl
End Sub