How to: Programmatically Define and Select Ranges in Documents

 

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 M:Microsoft.Office.Interop.Word.Range.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 a VSTO Add-in).

Applies to: The information in this topic applies to document-level projects and VSTO add-in projects for Word. For more information, see Features Available by Office Application and Project Type.

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

  1. 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.

    No code example is currently available or this language may not be supported.

To define a range by using a VSTO Add-in

  1. Add the range to the document by passing a start and end character to the M:Microsoft.Office.Interop.Word._Document.Range(System.Object@,System.Object@) 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.

    No code example is currently available or this language may not be supported.

The following examples show how to select the entire document by using the M:Microsoft.Office.Interop.Word.Range.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

  1. Use the M:Microsoft.Office.Interop.Word.Range.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.

    No code example is currently available or this language may not be supported.

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

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

    No code example is currently available or this language may not be supported.

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

To select a sentence in the active document

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

    No code example is currently available or this language may not be supported.

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.

    No code example is currently available or this language may not be supported.

  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.

    No code example is currently available or this language may not be supported.

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

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

  1. Use the M:Microsoft.Office.Interop.Word.Range.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.

    No code example is currently available or this language may not be supported.

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

  1. Use the P:Microsoft.Office.Interop.Word._Document.Content property to define a range that encompasses the entire document.

    No code example is currently available or this language may not be supported.

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

To select a sentence in the active document

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

    No code example is currently available or this language may not be supported.

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.

    No code example is currently available or this language may not be supported.

  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.

    No code example is currently available or this language may not be supported.

Word Object Model Overview
How to: Programmatically Extend Ranges in Documents
How to: Programmatically Retrieve Start and End Characters in Ranges
How to: Programmatically Extend Ranges in Documents
How to: Programmatically Reset Ranges in Word Documents
How to: Programmatically Collapse Ranges or Selections in Documents
How to: Programmatically Exclude Paragraph Marks When Creating Ranges

Show: