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 Select method of the Range object, or by using the Content property of the Document class (in a document-level customization) or the 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.
Define 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 in a document-level customization
Add the range to the document by passing a start and end character to the Range method of the Document class. To use this code example, run it from the
ThisDocument
class in your project.
To define a range by using a VSTO Add-in
Add the range to the document by passing a start and end character to the Range method of the 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.
Select a range in a document-level customization
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 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.
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.
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.
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
Create a range variable.
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.
Select a range by using a VSTO Add-in
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 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.
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.
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.
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
Create a range variable.
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.
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(); }
Related content
- 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