Share via


Creating, Defining, and Redefining a Range

You typically create a Range object by declaring an object variable of type Range and then instantiating that variable by using either the Document object's Range method or the Range property of another object, such as a Character, Word, Sentence, or Selection object. For example, the following code creates two Range objects that both represent the second sentence in the active document.

Public Sub GetRangeExample()
   ' This example shows how the Range method and the Range
   ' property both return the same characters.
   
   Dim rngRangeMethod        As Word.Range
   Dim rngRangeProperty      As Word.Range
   
   With ActiveDocument
      If .Sentences.Count >= 2 Then
            Set rngRangeMethod = .Range(.Sentences(2).Start, _
                .Sentences(2).End)
            Set rngRangeProperty = .Sentences(2)
      End If
   End With
   
   Debug.Print rngRangeMethod.Text
   Debug.Print rngRangeProperty.Text
End Sub

When you use the Range method to specify a specific area of a document, you use the method's Start argument to specify the character position where the range should begin and you use the End argument to specify where the range should end. The first character in a document is at character position 0. The last character position is equal to the total number of characters in the document. You can determine the number of characters in a document by using the Characters collection's Count property. As shown in the preceding example, you can also use the Start and End properties of a Bookmark, Selection, or Range object to specify the Range method's Start and End arguments. You can set the Start and End arguments to the same number. In this case, you create a range that does not include any characters.

You can set or redefine the contents of a Range object by using the object's SetRange method. You can specify or redefine the start of a range by using the Range object's Start property or its MoveStart method. Likewise, you can specify or redefine the end of a range by using the Range object's End property or its MoveEnd method.

The following example begins by using the Content property to create a Range object that covers the entire contents of a document. It then changes the End property to specify that the end of the range will be at the end of the first sentence in the document. It then uses the SetRange method to redefine the range to cover the first paragraph in the document. Finally, it uses the MoveEnd method to extend the end of the range to the end of the second paragraph in the document. At each step in the example, the number of characters contained in the range is printed to the Immediate window.

Public Sub RedefineRangeExample1()
   ' This procedure illustrates how to use various properties
   ' and methods to redefine the contents of a Range object.
   ' See also the RedefineRangeExample2 procedure.
   Dim rngSample As Range
   
   Set rngSample = ActiveDocument.Content
   
   With rngSample
      Debug.Print "The range now contains " & .Characters.Count _
            & " characters."
      .End = ActiveDocument.Sentences(1).End
      Debug.Print "The range now contains " & .Characters.Count _
            & " characters."
      .SetRange Start:=0, End:=ActiveDocument.Paragraphs(1).Range.End
      Debug.Print "The range now contains " & .Characters.Count _
            & " characters."
      .MoveEnd Unit:=wdParagraph, Count:=1
      Debug.Print "The range now contains " & .Characters.Count _
            & " characters."
   End With
End Sub

You can also redefine a Range object by using the object's Find property to return a Find object. The following example illustrates the use of the Find property to locate text within the active document. If the text is found, the Range object is automatically redefined to contain the text that matched the search criteria.

With rngRangeText.Find
   .ClearFormatting
   If .Execute(FindText:=strTextToFind) Then
      Set RedefineRangeExample2 = rngRangeText
   Else
      Set RedefineRangeExample2 = Nothing
   End If
End With

Many Word objects have a Range property that returns a Range object. You use an object's Range property to return a Range object under circumstances where you must work with properties or methods of the Range object that are not available from the object itself. For example, the following code uses the Range property of a Paragraph object to return a Range object that is used to format the text in the first paragraph in a document:

Dim rngPara As Range

Set rngPara = ActiveDocument.Paragraphs(1).Range
With rngPara
   .Bold = True
   .ParagraphFormat.Alignment = wdAlignParagraphCenter
   .Font.Name = "Arial"
End With

After you identify the Range object, you can apply methods and properties of the object to modify the contents of the range or get information about the range. You use the Range object's StoryType property to determine where in the document the Range is located.

See Also

Working with Microsoft Word Objects | The Range Object | Working with Text in a Range Object | Determining Where the Range Is Located | Inserting Text in a Range | Understanding Paragraph Marks