HOW TO: Programmatically Obtain Paragraph Numbers Generated by a Style in Word 2000 (315728)



The information in this article applies to:

  • Microsoft Word 2000

This article was previously published under Q315728

SUMMARY

This step-by-step article explains how to extract numbering information from paragraphs of a Word document in which the numbering was created automatically by using paragraph formatting or a style sheet.

Microsoft Word offers a programming model that exposes the text and structure of a document. The Paragraph object represents a paragraph in your document, and the Range object represents a contiguous portion of text. Range has a property named Text, which gives you the text in the Range object.

Range also has a ListFormat object that provides list and number formatting. ListFormat has properties named ListString and ListLevelNumber, which tell you the automatically generated paragraph numbers and the indent level, respectively.

back to the top

Requirements

The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs that are required.

Prior knowledge required:
  • Programming Microsoft Word 2000 by using Microsoft Visual Basic, Applications Edition
  • Familiarity with the automatic numbering features of Microsoft Word
back to the top

Create a Word Document with Numbered Paragraphs

  1. Start Word 2000, and then create a new document.
  2. Type the following words, separated by paragraph returns, as follows:

    Animal
    Cat
    Dog
    Vegetable
    Mineral

  3. Select the text. On the Format menu, click Bullets and Numbering. On the Numbered tab, select the first numbered format, and then click OK.
  4. Select the second and third lines of text in your document. On the Formatting toolbar, click the Increase Indent button. The document now appears as follows:

    1. Animal
      a. Cat
      b. Dog

      VegetableMineral
    2. VegetableMineral
    3. Mineral
  5. Save the document as WordNum.doc.
back to the top

Write a Word Macro to Display Numbered Paragraphs

  1. On the Tools menu, click Macro, and then click Visual Basic Editor. The Visual Basic Editor appears.
  2. In the Project window, double-click ThisDocument to start the code editor.
  3. Type the following code in the code editor. This code iterates through all the paragraphs in the active document and displays the text, list number, and indent level for each paragraph:
    Option Explicit
    Sub showNumbers()
    
        Dim objPara As Paragraph
        Dim sText As String
        Dim sList As String
        Dim nLevel As Integer
        For Each objPara In ActiveDocument.Paragraphs
            With objPara.Range
                sText = .Text
                sList = .ListFormat.ListString
                nLevel = .ListFormat.ListLevelNumber     
                MsgBox "Text = " & sText & _
                    "List = " & sList & " Level = " & nLevel
            End With
        Next
    End Sub
    					
back to the top

Verification

  1. Press F5 to run the macro.
  2. A message box appears that displays the text, list number, and indent level for the first paragraph in your document:

    Text = Animal
    List = 1. Level = 1

  3. Click OK to dismiss the message box.
  4. Four more message boxes appear in sequence to display the following information about the other paragraphs in your document:

    Text = Cat
    List = a. Level = 2

    Text = Dog
    List = b. Level = 2

    Text = Vegetable
    List = 2. Level = 1

    Text = Mineral
    List = 3. Level = 1

back to the top

Modification Type:MinorLast Reviewed:1/6/2006
Keywords:kbhowto kbHOWTOmaster KB315728 kbAudDeveloper