Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Word Solutions
 How to: Extend Ranges in Documents
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0)
How to: Extend Ranges in Documents

Updated: November 2007

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

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

  • wdCharacter()()()

  • wdWord()()()

  • wdSentence()()()

  • wdParagraph()()()

  • wdSection()()()

  • wdStory()()()

  • wdCell()()()

  • wdColumn()()()

  • wdRow()()()

  • wdTable()()()

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: Define and Select Ranges in Documents.

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

    Visual Basic
    Dim rng As Word.Range = Me.Range(Start:=0, End:=7)
    
    
    C#
    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 example uses the active document.

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

    Visual Basic
    rng.MoveStart(Unit:=Word.WdUnits.wdCharacter, Count:=7)
    
    
    C#
    object unit = Word.WdUnits.wdCharacter;
    object count = 7;
    rng.MoveStart(ref unit, ref count);
    
    
  3. Use the MoveEnd(Object%, Object%) method of the Range object to move the end position of the range.

    Visual Basic
    rng.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=7)
    
    
    C#
    unit = Word.WdUnits.wdCharacter;
    count = 7;
    rng.MoveEnd(ref unit, ref count);
    
    

To extend a range in a document-level customization

  • 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.

    Visual Basic
    ' 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)
    
    
    C#
    // 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.
    object unit = Word.WdUnits.wdCharacter;
    object count = 7;
    rng.MoveStart(ref unit, ref count);
    
    // Move the end position 7 characters.
    unit = Word.WdUnits.wdCharacter;
    count = 7;
    rng.MoveEnd(ref unit, ref count);
    
    

To extend a range in an application-level add-in

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

    Visual Basic
    ' 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)
    
    
    C#
    // Define a range of 7 characters.
    object start = 0;
    object end = 7;
    Word.Range rng = this.Application.ActiveDocument.Range(
        ref start, ref end);
    
    // Move the start position 7 characters.
    object unit = Word.WdUnits.wdCharacter;
    object count = 7;
    rng.MoveStart(ref unit, ref count);
    
    // Move the end position 7 characters.
    unit = Word.WdUnits.wdCharacter;
    count = 7;
    rng.MoveEnd(ref unit, ref count);
    
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker