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.

Sub BorderAroundFirstParagraph() 
 Selection.Paragraphs(1).Borders.Enable = True 
End Sub

The following example applies a border around each paragraph in the selection.

Sub BorderAroundSelection() 
 Selection.Paragraphs.Borders.Enable = True 
End Sub

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

Sub ShadeTableRow() 
 Selection.Tables(1).Rows(1).Shading.Texture = wdTexture10Percent 
End Sub

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.

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

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.

Sub ShadeAllFirstRowsInTables() 
 Dim tblTable As Table 
 If Selection.Tables.Count >= 1 Then 
 For Each tblTable In Selection.Tables 
 tblTable.Rows(1).Shading.Texture = wdTexture30Percent 
 Next tblTable 
 End If 
End Sub

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.