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
- Start Word 2000, and then create a new document.
- Type the following words, separated by paragraph returns, as follows:
Animal
Cat
Dog
Vegetable
Mineral
- Select the text. On the Format menu, click Bullets and Numbering. On the Numbered tab, select the first numbered format, and then click OK.
- 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:
- Animal
a. Cat
b. Dog
VegetableMineral - VegetableMineral
- Mineral
- Save the document as WordNum.doc.
back to the top
Write a Word Macro to Display Numbered Paragraphs
- On the Tools menu, click Macro, and then click Visual Basic Editor. The Visual Basic Editor appears.
- In the Project window, double-click ThisDocument to start the code editor.
- 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
- Press F5 to run the macro.
- 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
- Click OK to dismiss the message box.
- 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