Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

Can I select a group of tables to format in microsoft word using VBA?

I'm looking for a way to quickly format groups of tables in documents. Unfortunately I can't just format all tables as I have to leave some unmodified (usually in the appendices, but sometimes at the beginning).

What I have so far will allow me to format all tables from Table 4 to the end. I can't figure out how to select tables 4 through 78 for example. I also need all of the cells to be vertically oriented in the middle. I've spent an ungodly amount of my life formatting each individual table, so any help would be GREATLY APPRECIATED.

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
question from:https://stackoverflow.com/questions/65947672/can-i-select-a-group-of-tables-to-format-in-microsoft-word-using-vba

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For example:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...