Word Macro: Repeat Table Headings

This Word VBA macro repeats all table headings.

Macro Example

Sub RepeatTableHeadings()

  Dim tbl As Table

  For Each tbl In ActiveDocument.Tables
    tbl.Rows(1).HeadingFormat = True
  Next tbl

End Sub

Description

Variable Declaration

Sub...

  Dim tbl As Table

The 3rd line declares an object variable named tbl that will refer to a Table object.

Starts Looping

Sub...

  Dim tbl As Table

  For Each tbl In ActiveDocument.Tables

The 5th line starts to loop through all the elements (Table) of a collection (Document.Tables).

[Application.]ActiveDocument

  For Each tbl In ActiveDocument...

The [Application.]ActiveDocument property returns an active Document object.

obj-Word-Application-ActiveDocument

Because the ActiveDocument property is a member of the Word.Global class, we can omit the Application property.

obj-Word-ActiveDocument

ActiveDocument Tables (Document.Tables)

  For Each tbl In ActiveDocument.Tables

The ActiveDocument.Tables (Document.Tables property) returns the Tables collection object in the active document.

obj-Word-Document-Tables

The Tables is a collection of the Table object.
And the Tables collection object represents the tables in a selection, range, or document.

obj-Word-Tables

Repeat Headings of Each Table

Sub...

  Dim tbl As Table

  For Each tbl In ActiveDocument.Tables
    tbl.Rows(1).HeadingFormat = True

The 6th line adds the table heading format to the first table row.

Table.Rows

    tbl.Rows...

The tbl.Rows (Table.Rows property) returns the Rows collection that represents all the table rows within a table.

obj-Word-Table-Rows

Rows(1) (Rows.Item(1))

    tbl.Rows(1)...

The Rows.Item method returns an individual Row object in the Rows collection object. And the Rows.Item(1) represents the first row.

obj-Word-Rows-Item

The Item method is a default member of the Rows collection object. We can see a little blue marble beside its icon in the Object Browser. So we can omit the .Item.

Rows(1).HeadingFormat (Row.HeadingFormat)

    tbl.Rows(1).HeadingFormat... 

The Rows(1).HeadingFormat (Row.HeadingFormat property) returns True if the specified row are formatted as a table heading.

obj-Word-Row-HeadingFormat

Loops Back

The 7th line loops back to get the next table.

Sub...

  Dim tbl As Table

  For Each tbl In ActiveDocument.Tables
    tbl.Rows(1).HeadingFormat = True
  Next tbl

After every table is evaluated, this macro ends.

Sub RepeatTableHeadings()

  Dim tbl As Table

  For Each tbl In ActiveDocument.Tables
    tbl.Rows(1).HeadingFormat = True
  Next tbl

End Sub

Properties

http://www.relief.jp/itnote/archives/word-macro-heading-rows-repeat.php

Apply To

  • Word 2013
  • Word 2010
  • Word 2007