We recommend using Visual Studio 2017

How to: Programmatically Collapse Ranges or Selections in Documents

 

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 T:Microsoft.Office.Interop.Word.WdCollapseDirection enumeration 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.

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

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

  2. Use the F:Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseStart enumeration value to collapse the range.

                rng.Collapse(Word.WdCollapseDirection.wdCollapseStart); 
    

  3. Insert the new text.

                rng.Text = " New Text ";
    

  4. Select the Range.

                rng.Select();
    

If you use the F:Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd enumeration value, the text is inserted at the beginning of the following paragraph.

            rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd);

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: Programmatically Exclude Paragraph Marks When Creating Ranges.

To collapse a range in a document-level customization

  1. 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; 
    
                rng.Collapse(Word.WdCollapseDirection.wdCollapseStart); 
    
                rng.Text = " New Text ";
                rng.Select();
    

To collapse a range in an VSTO Add-in

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

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

How to: Programmatically Insert Text into Word 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
How to: Programmatically Extend Ranges in Documents
How to: Programmatically Reset Ranges in Word Documents

Show: