我可以 select 一组表格使用 VBA 在 microsoft word 中格式化吗?
Can I select a group of tables to format in microsoft word using VBA?
我正在寻找一种快速格式化文档中 table 组的方法。不幸的是,我不能只格式化所有 table,因为我必须保留一些未修改的内容(通常在附录中,但有时在开头)。
我目前所拥有的将允许我格式化从 Table 4 到最后的所有 table。例如,我不知道如何 select tables 4 到 78。我还需要将所有单元格垂直放置在中间。我一生中花费了大量时间来格式化每个人 table,因此我们将不胜感激任何帮助。
Sub FormatTables()
Dim TableIndex As Long
Dim Mytable As Table
For TableIndex = 4 To ActiveDocument.tables.Count
Set Mytable = ActiveDocument.tables(TableIndex)
With Mytable
.Range.Style = ActiveDocument.Styles("TableText Arial 9")
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Rows.Alignment = wdAlignRowCenter
.Rows.Height = InchesToPoints(0)
.TopPadding = InchesToPoints(0)
.BottomPadding = InchesToPoints(0)
.LeftPadding = InchesToPoints(0.08)
.RightPadding = InchesToPoints(0.08)
.Spacing = 0
.AllowPageBreaks = True
.AutoFitBehavior (wdAutoFitWindow)
End With
Next TableIndex
End Sub
- 从您的程序中删除
Set Mytable = ActiveDocument.tables(TableIndex)
。
- 从您的程序中删除
Dim Mytable As Table
。
- 将程序的声明行替换为
Private Sub FormatTable(Mytable As Table)
(名称中的单数“table”)。
- 创建一个新过程,如下所示。
这里:
Sub FormatTables() ' plural "tables"
Dim i As Integer
For i = 4 To 78
FormatTable ActiveDocument.Tables(i)
Next i
End Sub
请注意 table 是按照它们出现的顺序编入索引的。如果插入 table,则插入项下方的所有 table 的索引都会更改。如果这是您项目中的一个问题,您可以使用 Title
属性 为 table 分配名称。然后,您可以通过头衔称呼个人 table。
例如:
Sub FormatTables()
Application.ScreenUpdating = False
Dim TableIndex As Long
For TableIndex = 4 To 78
With ActiveDocument.Tables(TableIndex)
.Range.Style = "TableText Arial 9"
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Rows.Alignment = wdAlignRowCenter
'Format the paragraphs centrally as well
'.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Rows.Height = 0
.TopPadding = 0
.BottomPadding = 0
.LeftPadding = InchesToPoints(0.08)
.RightPadding = InchesToPoints(0.08)
.Spacing = 0
.AllowPageBreaks = True
.AutoFitBehavior (wdAutoFitWindow)
End With
Next TableIndex
Application.ScreenUpdating = True
End Sub
我正在寻找一种快速格式化文档中 table 组的方法。不幸的是,我不能只格式化所有 table,因为我必须保留一些未修改的内容(通常在附录中,但有时在开头)。
我目前所拥有的将允许我格式化从 Table 4 到最后的所有 table。例如,我不知道如何 select tables 4 到 78。我还需要将所有单元格垂直放置在中间。我一生中花费了大量时间来格式化每个人 table,因此我们将不胜感激任何帮助。
Sub FormatTables()
Dim TableIndex As Long
Dim Mytable As Table
For TableIndex = 4 To ActiveDocument.tables.Count
Set Mytable = ActiveDocument.tables(TableIndex)
With Mytable
.Range.Style = ActiveDocument.Styles("TableText Arial 9")
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Rows.Alignment = wdAlignRowCenter
.Rows.Height = InchesToPoints(0)
.TopPadding = InchesToPoints(0)
.BottomPadding = InchesToPoints(0)
.LeftPadding = InchesToPoints(0.08)
.RightPadding = InchesToPoints(0.08)
.Spacing = 0
.AllowPageBreaks = True
.AutoFitBehavior (wdAutoFitWindow)
End With
Next TableIndex
End Sub
- 从您的程序中删除
Set Mytable = ActiveDocument.tables(TableIndex)
。 - 从您的程序中删除
Dim Mytable As Table
。 - 将程序的声明行替换为
Private Sub FormatTable(Mytable As Table)
(名称中的单数“table”)。 - 创建一个新过程,如下所示。
这里:
Sub FormatTables() ' plural "tables"
Dim i As Integer
For i = 4 To 78
FormatTable ActiveDocument.Tables(i)
Next i
End Sub
请注意 table 是按照它们出现的顺序编入索引的。如果插入 table,则插入项下方的所有 table 的索引都会更改。如果这是您项目中的一个问题,您可以使用 Title
属性 为 table 分配名称。然后,您可以通过头衔称呼个人 table。
例如:
Sub FormatTables()
Application.ScreenUpdating = False
Dim TableIndex As Long
For TableIndex = 4 To 78
With ActiveDocument.Tables(TableIndex)
.Range.Style = "TableText Arial 9"
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Rows.Alignment = wdAlignRowCenter
'Format the paragraphs centrally as well
'.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Rows.Height = 0
.TopPadding = 0
.BottomPadding = 0
.LeftPadding = InchesToPoints(0.08)
.RightPadding = InchesToPoints(0.08)
.Spacing = 0
.AllowPageBreaks = True
.AutoFitBehavior (wdAutoFitWindow)
End With
Next TableIndex
Application.ScreenUpdating = True
End Sub