Compartilhar via


How to: Programmatically retrieve start and end characters in ranges

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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 Start and 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.

    Dim rng As Word.Range = Me.Sentences(2)
    
    Dim startPosition As String = rng.Start.ToString()
    Dim endPosition As String = rng.End.ToString()
    
    MessageBox.Show("Start: " & startPosition & " End: " & endPosition, "Range Information")
    
    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 a VSTO Add-in

  1. Get the values of the Start and 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.

    Dim rng As Word.Range = Me.Application.ActiveDocument.Sentences(2)
    
    Dim startPosition As String = rng.Start.ToString()
    Dim endPosition As String = rng.End.ToString()
    
    MessageBox.Show("Start: " & startPosition & " End: " & endPosition, "Range Information")
    
    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");
    

See also