Word VBA: Getting Selected Paragraphs Count

The following Word VBA macro shows a message box with the selected paragraphs count.

Macro Example

Sub GetSelectedParagraphsCount()

  MsgBox Selection.Paragraphs.Count

End Sub

Description

[Application.]Selection

  Selection...

The Application.Selection property returns the Selection object that represents a selected range or the insertion point.

obj-Word-Application-Selection

The Selection property is a member of the Word.Global class, so we can omit the Application property.

obj-Word-Global-Selection

Selection.Paragraphs

  Selection.Paragraphs...

The Selection.Paragraphs property returns the Paragraphs collection object that represents all the paragraphs in the specified selection.

obj-Word-Selection-Paragraphs

Paragraphs.Count

  Selection.Paragraphs.Count

The Paragraphs.Count property returns a Long integer value that represents the number of paragraphs.

obj-Word-Paragraphs-Count

MsgBox function

The code: Selection.Paragraphs.Count is set to the parameter of a MsgBox function.

Sub GetSelectedParagraphsCount()

  MsgBox Selection.Paragraphs.Count

End Sub

So, a message box shows the selected paragraphs count.

Properties

http://www.relief.jp/itnote/archives/word-vba-get-paragraphs-count.php

Apply To

  • Word 2013
  • Word 2010
  • Word 2007