Microsoft KB Archive/315728

= HOW TO: Programmatically Obtain Paragraph Numbers Generated by a Style in Word 2000 =

Article ID: 315728

Article Last Modified on 1/6/2006

-

APPLIES TO


 * Microsoft Word 2000 Standard Edition

-



This article was previously published under Q315728





IN THIS TASK
SUMMARY
 * Requirements
 * Create a Word Document with Numbered Paragraphs
 * Write a Word Macro to Show Numbered Paragraphs
 * Verification



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</li> Mineral</li></ol> </li> Save the document as WordNum.doc.</li></ol>

back to the top

Write a Word Macro to Display Numbered Paragraphs
<ol> On the Tools menu, click Macro, and then click Visual Basic Editor. The Visual Basic Editor appears.</li> In the Project window, double-click ThisDocument to start the code editor.</li>  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 &quot;Text = &quot; & sText & _ &quot;List = &quot; & sList & &quot; Level = &quot; & nLevel End With Next End Sub </li></ol>

back to the top

Verification
<ol> Press F5 to run the macro.</li> 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

</li> Click OK to dismiss the message box.</li> 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

</li></ol>

back to the top

Keywords: kbhowto kbhowtomaster KB315728

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.