WD: Macros to Count the Number of Document Paragraphs and Lines

ID: Q92929

The information in this article applies to:

SUMMARY

The number of paragraphs and lines in a document are counted and displayed in the Document Properties dialog box (Word versions 7.x) or in the Summary Info dialog box of earlier Word versions. To access this information from a macro, see the following examples.

MORE INFORMATION

NOTE: Microsoft provides macros "as is" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Word Versions 6.x and 7.x

To return the number of paragraphs in a document, use the following macro:

   Sub MAIN
      FileSummaryInfo .Update
      Dim dlg As FileSummaryInfo
      GetCurValues dlg
      para$ = dlg.NumParas
      MsgBox "There are " + para$ + " paragraphs in the document."
   End Sub

To return the number of lines in a document:

   Sub MAIN
      FileSummaryInfo .Update
      Dim dlg As FileSummaryInfo
      GetCurValues dlg
      line$ = dlg.NumLines
      MsgBox "There are " + line$ + " lines in the document."
   End Sub

Word version 2.x for Windows

To create a WordBasic macro that can be used to count the number of paragraphs or lines in a Word version 2.0 document and display the result in a message box, do the following:

1. From the Tools menu, choose Macro, and type a name, such as CountPara or

   CountLines, for the macro.

2. Click the Edit button.

3. Between Sub MAIN and End Sub, type the appropriate macro commands:

    - To count the paragraphs in a Word document:

         StartOfDocument
         While ParaDown()
         count = count + 1
         Wend
         MsgBox "There are" + Str$(count) + " paragraphs in the document."

    - To count the lines in a Word document:

         StartOfDocument
         While LineDown()
         count = count + 1
         Wend
         MsgBox "There are" + Str$(count) + " lines in the document."

4. From the File menu, choose Close, and save changes to the macro.

REFERENCES

"Microsoft Word for Windows and OS/2 Technical Reference," pages 103-

 107
"Using WordBasic," by WexTech Systems and Microsoft, pages 251 and 267

Additional query words: LineDown ParaDown While Version : 2.x 6.x 7.0 7.0a | 6.0 6.

Last Reviewed: July 30, 1997