Determine the index number of the current paragraph, table, section...

Or of any object that has a Range property

Article contributed by Dave Rado,
with acknowledgments to Andrew Gabb, Jonathan West and Ibby

Note that none of the following applies reliably if you are in Outline View; if you are, you need to change to some other view, such as Normal view, first.

1.

In order to operate on the currently selected paragraph, table, section, etc.

The first questions is – why do you need to know the index number?  If you want to know in order to operate on the currently selected paragraph, table or other object, you can simply use:

Selection.CollectionName(1)

For example:

Selection.Paragraphs(1).Range.Font.Bold = True

Selection.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "Hello"

Selection.Tables(1).Borders.Enable = False

All the above operate on the currently selected object.

Similarly, if you were working with a range variable, you could use:

MyRange.Paragraphs(1).Range.Font.Bold = True

Etc.

However if you really do need to know the index number, use the following method:

  

2.

Get the index number, by setting a range to the start of the document

The fastest and simplest way to get the index number, by far, is to set a range from the start of the document to the end of the first selected paragraph (or other object); and then use the Count property, as follows:

MsgBox ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count

MsgBox ActiveDocument.Range(0, Selection.Sections(1).Range.End).Sections.Count

MsgBox ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count

Again, you could also use this method to get the index number of a range rather than of a selection, as follows:

MsgBox ActiveDocument.Range(0, MyRange.Paragraphs(1).Range.End).Paragraphs.Count

Etc.

This method was suggested in the newsgroups by Andrew Gabb.