Word VBA: Counting Paragraphs in a Document

Sometimes, I must count paragraphs in a document when I make a Word macro.

Macro Example

The following Word VBA macro shows a message box with the number of paragraphs in an active document.

Sub GetParagraphsCount()

  MsgBox ActiveDocument.Paragraphs.Count

End Sub

Description

ActiveDocument property

  ActiveDocument...

The Application.ActiveDocument property returns an active Document object.

obj-Word-Application-ActiveDocument

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

obj-Word-ActiveDocument

Document.Sections property

  ActiveDocument.Paragraphs...

The Document.Paragraphs property (ActiveDocument.Paragraphs) returns the Paragraphs collection object that represents all the paragraphs in a document.

obj-Word-Document-Paragraphs

Paragraphs.Count property

  ActiveDocument.Paragraphs.Count

The Paragraphs.Count property returns a long integer value that represents the number of paragraphs in a document.

obj-Word-Paragraphs-Count

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

Sub GetParagraphsCount()

  MsgBox ActiveDocument.Paragraphs.Count

End Sub

So, a message box shows the count of paragraphs.

Properties

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

Apply To

  • Word 2013
  • Word 2010
  • Word 2007