Export (0) Print
Expand All

How to: Collapse Ranges or Selections in 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.

If you are working with a Range or Selection object, you might want to change the selection to an insertion point before inserting text, to avoid overwriting existing text. Both the Range and Selection objects have a Collapse method, which makes use of the WdCollapseDirection enumeration values:

  • wdCollapseStart - collapses the selection to the beginning of the selection. This is the default if you do not specify an enumeration value.

  • wdCollapseEnd - collapses the selection to the end of the selection.

To collapse a range and insert new text

  1. Create a Range object that consists of the first paragraph in the document.

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

    Word.Range rng = this.Paragraphs[1].Range; 
    

    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.Paragraphs[1].Range;
    
  2. Use the wdCollapseStart enumeration value to collapse the range.

    object direction = Word.WdCollapseDirection.wdCollapseStart;
    rng.Collapse(ref direction); 
    
  3. Insert the new text.

    rng.Text = " New Text ";
    
  4. Select the Range.

    rng.Select();
    

If you use the wdCollapseEnd enumeration value, the text is inserted at the beginning of the following paragraph.

direction = Word.WdCollapseDirection.wdCollapseEnd;
rng.Collapse(ref direction);

You might expect that inserting a new sentence would insert it before the paragraph marker, but that is not the case because the original range includes the paragraph marker. For more information, see How to: Exclude Paragraph Marks When Creating Ranges.

To collapse a range in a document-level customization

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

    Word.Range rng = this.Paragraphs[1].Range; 
    
    object direction = Word.WdCollapseDirection.wdCollapseStart;
    rng.Collapse(ref direction); 
    
    rng.Text = " New Text ";
    rng.Select();
    

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

  • The following example shows the complete method 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.Paragraphs[1].Range;
    
    object direction = Word.WdCollapseDirection.wdCollapseStart;
    rng.Collapse(ref direction);
    
    rng.Text = " New Text ";
    rng.Select();
    

Community Additions

ADD
Show:
© 2015 Microsoft