Referring to the active document element

To refer to the active paragraph, table, field, or other document element, use the Selection property to return a Selection object. From the Selection object, you can access all paragraphs in the selection or the first paragraph in the selection. The following example applies a border around the first paragraph in the selection.

Selection.Paragraphs(1).Borders.Enable = True

The following example applies a border around all the paragraphs in the selection.

Selection.Paragraphs.Borders.Enable = True

The following example applies shading to the first row of the first table in the selection.

Selection.Tables(1).Rows(1).Shading.Texture = wdTexture10Percent

An error occurs if the selection doesn't include a table. Use the Count property to determine if the selection includes a table. The following example applies shading to the first row of the first table in the selection.

If Selection.Tables.Count >= 1 Then
    Selection.Tables(1).Rows(1).Shading.Texture = wdTexture10Percent
Else
    MsgBox "Selection doesn't include a table"
End If

The following example applies shading to the first row of every table in the selection. The For Each...Next loop is used to step through the individual tables in the selection.

If Selection.Tables.Count >= 1 Then
    For Each aTable In Selection.Tables
        aTable.Rows(1).Shading.Texture = wdTexture10Percent
    Next aTable
End If