Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Word Solutions
 How to: Reset Ranges in Word Docume...

  Switch on low bandwidth view
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: Reset Ranges in Word Documents

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.

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

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.

    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 code 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 SetRange to start the range at the second sentence and end it at the end of the fifth sentence.

    Visual Basic
    rng.SetRange(Start:=Me.Sentences(2).Start, End:=Me.Sentences(5).End)
    
    
    C#
    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.

    Visual Basic
    Dim rng As Word.Range = Me.Range(Start:=0, End:=7)
    
    ' Reset the existing Range.
    rng.SetRange(Start:=Me.Sentences(2).Start, End:=Me.Sentences(5).End)
    rng.Select()
    
    
    C#
    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.

    Visual Basic
    Dim rng As Word.Range = Me.Application.ActiveDocument.Range(Start:=0, End:=7)
    
    ' Reset the existing Range.
    rng.SetRange(Start:=Me.Application.ActiveDocument.Sentences(2).Start, _
        End:=Me.Application.ActiveDocument.Sentences(5).End)
    rng.Select()
    
    
    C#
    object start = 0;
    object end = 7;
    Word.Range rng = this.Application.ActiveDocument.Range(
        ref start, ref end);
    
    // Reset the existing Range. 
    rng.SetRange(this.Application.ActiveDocument.Sentences[2].Start,
        this.Application.ActiveDocument.Sentences[5].End);
    rng.Select();
    
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker