WD2000: Macro to Count Number of Paragraphs and Lines in a Document (211455)



The information in this article applies to:

  • Microsoft Word 2000

This article was previously published under Q211455

SUMMARY

The number of paragraphs and lines in a document can be counted and displayed by using a Visual Basic for Applications macro.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

212536 OFF2000: How to Run Sample Code from Knowledge Base Articles

There are several methods that you can use to return the number of paragraphs for a document by using Visual Basic for Applications properties and collections. Use one of the following examples as appropriate for your situation:
Sub LinesAndParagraphsCount1()

' To Display the Total Number of Paragraphs in a document.

   Dim AD As Document
   Dim DP As Object

   Set AD = ActiveDocument
   Set DP = AD.BuiltInDocumentProperties

   ' Returns the number of paragraphs in a document.
   MsgBox "There are " & AD.Paragraphs.Count & " paragraphs."

End Sub
				

-or-

Sub LinesAndParagraphsCount2()

' To Display the Number of Paragraphs with text and all lines
' in the document.

   Dim AD As Document
   Dim DP As Object

   Set AD = ActiveDocument
   Set DP = AD.BuiltInDocumentProperties

   ' Returns the number of paragraphs and lines in a document.
   MsgBox "There are " & DP("Number Of Paragraphs") & _
      " paragraphs containing text " & "and " & DP("Number Of Lines") & _
      " lines counted."

End Sub
				

-or-

Sub LinesAndParagraphsCount3()

' Displays the Number of Paragraphs and Lines in the
' selected range of text.

   Dim AD As Document
   Dim DP As Object

   Set AD = ActiveDocument
   Set DP = AD.BuiltInDocumentProperties

   ' Returns the number of selected paragraphs and lines in a document.
   nParas = Selection.Paragraphs.Count
   nLines = Selection.Range.ComputeStatistics(Statistic:=wdStatisticLines)

   MsgBox "The selected range contains " & nParas & " paragraphs " & _
      vbCr & "and " & nLines & " of lines of text."

End Sub
				
NOTE: This is the same information that is provided in the Word Count dialog box (on the Tools menu, click Word Count) and the Statistics tab of the Document Properties dialog box (on the File menu, click Properties).

For additional information about checking for blank lines, click the article number below to view the article in the Microsoft Knowledge Base:

201956 WD2000: VBA Routines to Determine If Line or Document Is Empty

REFERENCES

For additional information about programming resources with Microsoft Word 2000, click the article numbers below to view the articles in the Microsoft Knowledge Base:

212623 WD2000: Macro Programming Resources

163435 VBA: Programming Resources for Visual Basic for Applications


Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto kbmacroexample kbProgramming KB211455