Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Word Solutions
 How to: Define and Select Ranges in...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0)
How to: Define and Select Ranges in Documents

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

You can define a range in a Microsoft Office Word document by using a Range object. 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 Microsoft.Office.Tools.Word..::.Document class (in a document-level customization) or the Microsoft.Office.Interop.Word..::.Document class (in an application-level add-in).

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 in a document-level customization

  • Add the range to the document by passing a start and end character to the Range method of the Microsoft.Office.Tools.Word..::.Document class. To use this code example, run it from the ThisDocument class in your project.

    Visual Basic
    Dim rng As Word.Range = Me.Range(Start:=0, End:=7)
    rng.Select()
    
    
    C#
    object start = 0; 
    object end = 7; 
    Word.Range rng = this.Range(ref start, ref end); 
    
    rng.Select();
    
    

To define a range by using an application-level add-in

  • Add the range to the document by passing a start and end character to the Range method of the Microsoft.Office.Interop.Word..::.Document class. The following code example adds a range to the active document. To use this code example, run it from the ThisAddIn class in your project.

    Visual Basic
    Dim rng As Word.Range = Me.Application.ActiveDocument.Range(Start:=0, End:=7)
    rng.Select()
    
    
    C#
    object start = 0;
    object end = 7;
    Word.Range rng = this.Application.ActiveDocument.Range(
        ref start, ref end);
    
    rng.Select();
    
    

The following examples show how to select the entire document by using the Select method of a Range object, or by using the Content property of the Microsoft.Office.Tools.Word..::.Document class.

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

  • Use the Select method of a Range that contains the entire document. To use the following code example, run it from the ThisDocument class in your project.

    Visual Basic
    Me.Range.Select()
    
    
    C#
    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 by using the Content property

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

    Visual Basic
    Me.Content.Select()
    
    
    C#
    this.Content.Select();
    
    

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

To select a sentence in the active document

  • Set the range by using the Sentences collection. Use the index of the sentence you want to select.

    Visual Basic
    Dim s2 As Word.Range = Me.Sentences(2)
    s2.Select()
    
    
    C#
    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.

    Visual Basic
    Dim rng As Word.Range
    
    
    C#
    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 then select the range.

    Visual Basic
    If Me.Sentences.Count >= 2 Then
    
        Dim startLocation As Object = Me.Sentences(2).Start
        Dim endLocation As Object = Me.Sentences(2).End
    
        ' Supply a Start and End value for the Range.
        rng = Me.Range(Start:=startLocation, End:=endLocation)
    
        ' Select the Range
        rng.Select()
    End If
    
    
    C#
    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 examples show how to select the entire document by using the Select method of a Range object, or by using the Content property of the Microsoft.Office.Interop.Word..::.Document class.

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

  • Use the Select method of a Range that contains the entire document. The following code example selects the contents of the active document. To use this code example, run it from the ThisAddIn class in your project.

    Visual Basic
    Me.Application.ActiveDocument.Range.Select()
    
    
    C#
    object start = this.Application.ActiveDocument.Content.Start;
    object end = this.Application.ActiveDocument.Content.End;
    
    this.Application.ActiveDocument.Range(ref start, ref end).Select();
    
    

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

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

    Visual Basic
    Me.Application.ActiveDocument.Content.Select()
    
    
    C#
    this.Application.ActiveDocument.Content.Select();
    
    

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

To select a sentence in the active document

  • Set the range by using the Sentences collection. Use the index of the sentence you want to select.

    Visual Basic
    Dim s2 As Word.Range = Me.Application.ActiveDocument.Sentences(2)
    s2.Select()
    
    
    C#
    Word.Range s2 = this.Application.ActiveDocument.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.

    Visual Basic
    Dim rng As Word.Range
    
    
    C#
    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 then select the range.

    Visual Basic
    Dim document As Word.Document = Me.Application.ActiveDocument
    If document.Sentences.Count >= 2 Then
    
        Dim startLocation As Object = document.Sentences(2).Start
        Dim endLocation As Object = document.Sentences(2).End
    
        ' Supply a Start and End value for the Range.
        rng = document.Range(Start:=startLocation, End:=endLocation)
    
        ' Select the Range
        rng.Select()
    End If
    
    
    C#
    Word.Document document = this.Application.ActiveDocument;
    
    if (document.Sentences.Count >= 2)
    {
        object startLocation = document.Sentences[2].Start;
        object endLocation = document.Sentences[2].End;
    
        // Supply a Start and End value for the Range. 
        rng = document.Range(ref startLocation, ref endLocation);
    
        // Select the Range.
        rng.Select();
    }
    
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker