How to: Programmatically Extend Ranges in Documents

 

After you define a Range object in a Microsoft Office Word document, you change its start and end points by using the M:Microsoft.Office.Interop.Word.Range.MoveStart(System.Object@,System.Object@) and M:Microsoft.Office.Interop.Word.Range.MoveEnd(System.Object@,System.Object@) methods. The M:Microsoft.Office.Interop.Word.Range.MoveStart(System.Object@,System.Object@) and M:Microsoft.Office.Interop.Word.Range.MoveEnd(System.Object@,System.Object@) methods take the same two arguments, Unit and Count. The Count argument is the number of units to move, and the Unit argument can be one of the following T:Microsoft.Office.Interop.Word.WdUnits values:

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 defines a seven-character range. It then moves the start position of the range seven characters after the original start position. Because the end position of the range was also seven characters after the start position, the result is a range that consists of zero characters. The code then moves the end position seven characters after the current end position.

To extend a range

  1. Define a range of characters. For more information, see How to: Programmatically Define and Select Ranges in Documents.

    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 VSTO Add-in. This example uses the active document.

                Word.Range rng = this.Application.ActiveDocument.Range(0, 7);
    

  2. Use the M:Microsoft.Office.Interop.Word.Range.MoveStart(System.Object@,System.Object@) method of the Range object to move the start position of the range.

                rng.MoveStart(Word.WdUnits.wdCharacter, 7);
    

  3. Use the M:Microsoft.Office.Interop.Word.Range.MoveEnd(System.Object@,System.Object@) method of the Range object to move the end position of the range.

                rng.MoveEnd(Word.WdUnits.wdCharacter, 7);
    

To extend a range in a document-level customization

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

                // Define a range of 7 characters.
                object start = 0;
                object end = 7;
                Word.Range rng = this.Range(ref start, ref end);
    
                // Move the start position 7 characters.
                rng.MoveStart(Word.WdUnits.wdCharacter, 7);
    
                // Move the end position 7 characters.
                rng.MoveEnd(Word.WdUnits.wdCharacter, 7);
    

To extend a range in an application-level VSTO Add-in

  1. The following example shows the complete code for an VSTO Add-in. To use this code, run it from the ThisAddIn class in your project.

                // Define a range of 7 characters.
                Word.Range rng = this.Application.ActiveDocument.Range(0, 7);
    
                // Move the start position 7 characters.
                rng.MoveStart(Word.WdUnits.wdCharacter, 7);
    
                // Move the end position 7 characters.
                rng.MoveEnd(Word.WdUnits.wdCharacter, 7);
    

How to: Programmatically Reset Ranges in Word Documents
How to: Programmatically Collapse Ranges or Selections in Documents
How to: Programmatically Define and Select Ranges in Documents
How to: Programmatically Retrieve Start and End Characters in Ranges
How to: Programmatically Exclude Paragraph Marks When Creating Ranges

Show: