Export (0) Print
Expand All

How to: Programmatically Reset Ranges in Word Documents

Use the SetRange method to resize an existing range in a Microsoft Office Word document.

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

To reset an existing range

  1. Set an initial range starting with the first seven characters in the document.

    The following code example can be used in a document-level customization.

    object start = 0; 
    object end = 7; 
    Word.Range rng = this.Range(ref start,ref end); 
    

    The following code example can be used in an application-level add-in. This code uses the active document.

    Word.Range rng = this.Application.ActiveDocument.Range(0, 7);
    
  2. Use SetRange to start the range at the second sentence and end it at the end of the fifth sentence.

    rng.SetRange(this.Sentences[2].Start, this.Sentences[5].End); 
    

To reset an existing range in a document-level customization

  • The following example shows the complete example for a document-level customization. To use this code, run it from the ThisDocument class in your project.

    object start = 0; 
    object end = 7; 
    Word.Range rng = this.Range(ref start,ref end); 
    
    // Reset the existing Range. 
    rng.SetRange(this.Sentences[2].Start, this.Sentences[5].End); 
    rng.Select();
    

To reset an existing range in an application-level add-in

  • The following example shows the complete example for an application-level add-in. To use this code, run it from the ThisAddIn class in your project.

    Word.Range rng = this.Application.ActiveDocument.Range(0, 7);
    
    // Reset the existing Range. 
    rng.SetRange(this.Application.ActiveDocument.Sentences[2].Start,
        this.Application.ActiveDocument.Sentences[5].End);
    rng.Select();
    
Show:
© 2014 Microsoft