Best Loop to Format Tables Automatically in Word

2021-06-25T16:35:05.36+00:00

I need the best loop format to place my table formatting statements inside in order to format all the tables within a document.

Office Visual Basic for Applications
Office Visual Basic for Applications
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Visual Basic for Applications: An implementation of Visual Basic that is built into Microsoft products.
1,502 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Haynes, Alethea (Peraton) (US Person) 1 Reputation point
    2021-06-30T19:06:54.893+00:00

    @John Korchok

    Sub Z_FormatProposalTableSubhead()
    '
    ' ProposalTableSubhead Macro
    '
    Dim oTable As Table

    For Each oTable In ActiveDocument.Tables
    Selection.Tables(1).Select
    Selection.Style = ActiveDocument.Styles("Table Text Left")
    With Selection.Tables(1)
    With .Borders(wdBorderLeft)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = -553582593
    End With
    With .Borders(wdBorderRight)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = -553582593
    End With
    With .Borders(wdBorderTop)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = -553582593
    End With
    With .Borders(wdBorderBottom)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = -553582593
    End With
    With .Borders(wdBorderHorizontal)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = 8354422
    End With
    With .Borders(wdBorderVertical)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = 8354422
    End With
    .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
    .Borders.Shadow = False
    End With
    With Options
    .DefaultBorderLineStyle = wdLineStyleSingle
    .DefaultBorderLineWidth = wdLineWidth075pt
    .DefaultBorderColor = 8354422
    End With
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.SelectRow
    Selection.Style = ActiveDocument.Styles("Table Header")
    With Selection.Cells
    With .Shading
    .Texture = wdTextureNone
    .ForegroundPatternColor = wdColorAutomatic
    .BackgroundPatternColor = 16116447
    End With
    With .Borders(wdBorderLeft)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = -553582593
    End With
    With .Borders(wdBorderRight)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = -553582593
    End With
    With .Borders(wdBorderTop)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = -553582593
    End With
    With .Borders(wdBorderBottom)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = 8354422
    End With
    With .Borders(wdBorderVertical)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = 8354422
    End With
    .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
    .Borders.Shadow = False
    End With
    With Options
    .DefaultBorderLineStyle = wdLineStyleSingle
    .DefaultBorderLineWidth = wdLineWidth150pt
    .DefaultBorderColor = -553582593
    End With
    Selection.HomeKey Unit:=wdLine
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.SelectRow
    Selection.Style = ActiveDocument.Styles("Table Subhead")
    With Selection.Cells
    With .Shading
    .Texture = wdTextureNone
    .ForegroundPatternColor = wdColorAutomatic
    .BackgroundPatternColor = 12840424
    End With
    With .Borders(wdBorderLeft)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = -553582593
    End With
    With .Borders(wdBorderRight)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = -553582593
    End With
    With .Borders(wdBorderTop)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = 8354422
    End With
    With .Borders(wdBorderBottom)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = 8354422
    End With
    With .Borders(wdBorderVertical)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = 8354422
    End With
    .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
    .Borders.Shadow = False
    End With
    With Options
    .DefaultBorderLineStyle = wdLineStyleSingle
    .DefaultBorderLineWidth = wdLineWidth075pt
    .DefaultBorderColor = 8354422
    End With
    Selection.HomeKey Unit:=wdLine
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.SelectRow
    With Selection.Cells
    With .Shading
    .Texture = wdTextureNone
    .ForegroundPatternColor = wdColorAutomatic
    .BackgroundPatternColor = 16119285
    End With
    With .Borders(wdBorderLeft)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = -553582593
    End With
    With .Borders(wdBorderRight)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth150pt
    .Color = -553582593
    End With
    With .Borders(wdBorderTop)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = 8354422
    End With
    With .Borders(wdBorderBottom)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = 8354422
    End With
    With .Borders(wdBorderVertical)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = 8354422
    End With
    .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
    .Borders.Shadow = False
    End With
    With Options
    .DefaultBorderLineStyle = wdLineStyleSingle
    .DefaultBorderLineWidth = wdLineWidth075pt
    .DefaultBorderColor = 8354422
    End With
    Next oTable
    End Sub

    0 comments No comments

  2. Haynes, Alethea (Peraton) (US Person) 1 Reputation point
    2021-06-30T20:27:45.17+00:00

    Thank you sir, the coding did work for all the tables that were in my document. But, it did not apply the outside borders to the tables. I will try to add it myself.

    I appreciate your help.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.