How to: Programmatically collapse ranges or selections in documents
Applies to: Visual Studio Visual 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
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.
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
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.
Dim rng As Word.Range = Me.Paragraphs(1).Range
Word.Range rng = this.Paragraphs[1].Range;
The following code example can be used in a VSTO Add-in. This code uses the active document.
Dim rng As Word.Range = Me.Application.ActiveDocument.Paragraphs(1).Range
Word.Range rng = this.Application.ActiveDocument.Paragraphs[1].Range;
Use the wdCollapseStart enumeration value to collapse the range.
rng.Collapse(Word.WdCollapseDirection.wdCollapseStart)
rng.Collapse(Word.WdCollapseDirection.wdCollapseStart);
Insert the new text.
rng.Text = " New Text "
rng.Text = " New Text ";
Select the Range.
rng.Select()
rng.Select();
If you use the wdCollapseEnd enumeration value, the text is inserted at the beginning of the following paragraph.
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
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.
Document-level customization example
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.Dim rng As Word.Range = Me.Paragraphs(1).Range rng.Collapse(Word.WdCollapseDirection.wdCollapseStart) rng.Text = " New Text " rng.Select()
Word.Range rng = this.Paragraphs[1].Range; rng.Collapse(Word.WdCollapseDirection.wdCollapseStart); rng.Text = " New Text "; rng.Select();
VSTO Add-in example
To collapse a range in a VSTO Add-in
The following example shows the complete method for a VSTO Add-in. To use this code, run it from the
ThisAddIn
class in your project.Dim rng As Word.Range = Me.Application.ActiveDocument.Paragraphs(1).Range rng.Collapse(Word.WdCollapseDirection.wdCollapseStart) rng.Text = " New Text " rng.Select()
Word.Range rng = this.Application.ActiveDocument.Paragraphs[1].Range; rng.Collapse(Word.WdCollapseDirection.wdCollapseStart); rng.Text = " New Text "; rng.Select();
See also
- 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