Partager via


How to: Programmatically Extend ranges in documents

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

After you define a Range object in a Microsoft Office Word document, you change its start and end points by using the MoveStart and MoveEnd methods. The MoveStart and MoveEnd 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 WdUnits values:

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.

    Dim rng As Word.Range = Me.Range(Start:=0, End:=7)
    
    object start = 0;
    object end = 7;
    Word.Range rng = this.Range(ref start, ref end);
    

    The following code example can be used in a VSTO Add-in. This example uses the active document.

    Dim rng As Word.Range = Me.Application.ActiveDocument.Range(Start:=0, End:=7)
    
    Word.Range rng = this.Application.ActiveDocument.Range(0, 7);
    
  2. Use the MoveStart method of the Range object to move the start position of the range.

    rng.MoveStart(Unit:=Word.WdUnits.wdCharacter, Count:=7)
    
    rng.MoveStart(Word.WdUnits.wdCharacter, 7);
    
  3. Use the MoveEnd method of the Range object to move the end position of the range.

    rng.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=7)
    
    rng.MoveEnd(Word.WdUnits.wdCharacter, 7);
    

Document-level customization code

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.
    Dim rng As Word.Range = Me.Range(Start:=0, End:=7)
    
    ' Move the start position 7 characters.
    rng.MoveStart(Unit:=Word.WdUnits.wdCharacter, Count:=7)
    
    ' Move the end position 7 characters.
    rng.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=7)
    
    // 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);
    

VSTO Add-in Code

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

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

    ' Define a range of 7 characters.
    Dim rng As Word.Range = Me.Application.ActiveDocument.Range(Start:=0, End:=7)
    
    ' Move the start position 7 characters.
    rng.MoveStart(Unit:=Word.WdUnits.wdCharacter, Count:=7)
    
    ' Move the end position 7 characters.
    rng.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=7)
    
    // 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);
    

See also