We recommend using Visual Studio 2017

How to: Programmatically Retrieve Start and End Characters in Ranges

 

This example demonstrates how you can retrieve the character positions of the start and end positions of a range.

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.

To retrieve start and end characters of a range in a document-level customization

  1. Get the values of the P:Microsoft.Office.Interop.Word.Range.Start and P:Microsoft.Office.Interop.Word.Range.End properties of the Range object. The following code example gets the start and end position of the second sentence in the document. To use this code example, run it from the ThisDocument class in your project.

                Word.Range rng = this.Sentences[2];
    
                string startPosition = rng.Start.ToString();
                string endPosition = rng.End.ToString();
    
                MessageBox.Show("Start: " + startPosition + " End: " + endPosition, "Range Information");
    

To retrieve start and end characters of a range by using an VSTO Add-in

  1. Get the values of the P:Microsoft.Office.Interop.Word.Range.Start and P:Microsoft.Office.Interop.Word.Range.End properties of the Range object. The following code example gets the start and end position of the second sentence in the active document. To use this code example, run it from the ThisAddIn class in your project.

                Word.Range rng = this.Application.ActiveDocument.Sentences[2];
    
                string startPosition = rng.Start.ToString();
                string endPosition = rng.End.ToString();
    
                MessageBox.Show("Start: " + startPosition + " End: " + endPosition, "Range Information");
    

How to: Programmatically Define and Select Ranges in Documents
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
How to: Programmatically Count Characters in Documents

Show: