Export (0) Print
Expand All
Expand Minimize
3 out of 5 rated this helpful - Rate this topic

How to: Define and Select Ranges in Documents

NoteNote

Some code examples in this topic use the this or Me keyword or the Globals class in a way that is specific to document-level customizations, or they rely on features of document-level customizations such as host controls. These examples can be compiled only if you have the required applications installed. For more information, see Features Available by Product Combination.

You can define a range in a Microsoft Office Word 2003 document by using the Range object to supply a start value and an end value. You can select the entire document in a number of ways, for example, by using the Select method of the Range object, or by using the Content property of the Document.

Defining a Range

The following example shows how to create a new Range object that includes the first seven characters in the active document, including non-printing characters. It then selects the text within the range.

To define a range

  • Add the range to the document by specifying a start and end character in the Range.

    object start = 0; 
    object end = 7; 
    Word.Range rng = this.Range(ref start, ref end); 
    
    rng.Select();
    
    

Selecting a Range

The following examples show how to select the entire document by using the Select method of the Range or by using the Content property of the Document.

To select the entire document as a range using the Select method

  • Use the Select method of the Range object for ThisDocument.

    object start = this.Content.Start;
    object end = this.Content.End;
    
    this.Range(ref start, ref end).Select();
    
    

To select the entire document as a range using the Content property

  • Use the Content property to define a range that encompasses the entire document.

    this.Content.Select();
    
    

You can also use the methods and properties of other objects to determine a range.

To select a sentence in the active document

  • Set the range using the Sentences collection, supplying the index of the sentence you want to select.

    Word.Range s2 = this.Sentences[2]; 
    s2.Select();
    
    

Another way to select a sentence is to manually set the start and end values for the range.

To select a sentence by manually setting the start and end values

  1. Create a range variable.

    Word.Range rng; 
    
    
  2. Check to see if there are at least two sentences in the document, set the Start and End arguments of the range and select the range.

    if (this.Sentences.Count >= 2) 
    {
        object startLocation = this.Sentences[2].Start; 
        object endLocation = this.Sentences[2].End; 
    
        // Supply a Start and End value for the Range. 
        rng = this.Range(ref startLocation, ref endLocation); 
    
        // Select the Range.
        rng.Select();
    }
    
    

The following example shows the entire procedure.

Example

private void RangeSelect() 
{ 
    Word.Range rng; 

    if (this.Sentences.Count >= 2) 
    {
        object startLocation = this.Sentences[2].Start; 
        object endLocation = this.Sentences[2].End; 

        // Supply a Start and End value for the Range. 
        rng = this.Range(ref startLocation, ref endLocation); 

        // Select the Range.
        rng.Select();
    }
    else 
    {
        return; 
    }
} 

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.