word vba编辑表格格式需要执行两次才正确

For Each aTable In Selection.Tables
        aTable.Range.Select
        Selection.Font.Size = 10
        Selection.Rows.Height = 17.01
        With Selection.ParagraphFormat
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 0
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpaceSingle
            .FirstLineIndent = CentimetersToPoints(0)
            .CharacterUnitFirstLineIndent = 0
            .LineUnitBefore = 0
            .LineUnitAfter = 0
            .WordWrap = True
        End With
    Next

我通过如上代码调整表格格式,有两个问题:
1、为什么第一次执行后with里面的代码都不能正确执行,然后再执行一次就全部都正确了?
2、如何设置字号为5号字?我通过宏录制,代码显示是10,但是执行完后,字号直接就是10了,而10和五号字好像还有点区别。

  1. 可能是因为第一次执行时,代码中的 Selection 对象还没有完全与表格范围建立联系,需要再次选择一下表格范围才能正确执行格式设置。为避免这个问题,可以将代码中的 Selection 对象改为直接使用 aTable.Range 对象来设置格式,这样就不需要再进行选择操作了。例如,可以将代码改为:
For Each aTable In Selection.Tables
    aTable.Range.Font.Size = 10
    aTable.Range.Rows.Height = 17.01
    With aTable.Range.ParagraphFormat
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 0
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceSingle
        .FirstLineIndent = CentimetersToPoints(0)
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
        .WordWrap = True
    End With
Next
  1. Word 中的字号是以磅为单位的,而不是以号数为单位。一般来说,5 号字的字号大小约为 10.5 磅。如果需要设置字号为 5 号字,可以将 Selection.Font.Size = 10 改为 Selection.Font.Size = 10.5。如果要设置为其他字号,也可以直接修改相应的数值即可。