我想制作一个宏来自动格式化表格的单元格。 (在 Word 2013 )
以宏的形式记录所需的操作会产生以下代码:
With Selection.Cells(1)
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.19)
.RightPadding = CentimetersToPoints(0.19)
.WordWrap = True
.FitText = False
End With
再次运行它只会将更改应用于第一个单元格(查看代码后不会感到惊讶)。
我已尝试在其周围编写适当的循环:
For Each c in Selection.Cells
....
Next c
但即使我切换Screenupdating
。
使用宏来实现它有更好的方法吗?
答案 0 :(得分:0)
由于我无法选择VBA中的所有单元格,解决方法是创建一个临时文档,以复制表格,格式化并复制回来。
答案 1 :(得分:-1)
Sub NimitTest02()
Dim oTbl As Table
Dim oCell As Cell
Dim i As Integer
Set oTbl = ActiveDocument.Tables(1)
For i = 1 To ActiveDocument.Tables(1).Columns.Count
For Each oRow In oTbl.Rows
Set oCell = oRow.Cells(i)
oCell.Select
With Selection.Cells(1)
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.19)
.RightPadding = CentimetersToPoints(0.19)
.WordWrap = True
.FitText = False
End With
Next
Next
End Sub