How to: Collapse Ranges or Selections in Documents
Note |
|---|
|
Some code examples in this topic use the this or Me keyword or the |
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 two WdCollapseDirection enumerations:
-
wdCollapseStart- collapses the selection to the beginning of the selection. This is the default if you do not specify an enumeration.
-
wdCollapseEnd - collapses the selection to the end of the selection.
To collapse a range and insert new text
-
Create a Range object consisting of the first paragraph in the document.
-
Use the wdCollapseStart enumeration to collapse the range.
-
Insert the new text.
-
Select the Range.
If you use the wdCollapseEnd enumeration, the text is inserted at the beginning of the following paragraph.
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.
The following example shows the complete method.
Example
See Also
Tasks
How to: Insert Text into Word DocumentsHow to: Define and Select Ranges in Documents
How to: Retrieve Start and End Characters in Ranges
How to: Exclude Paragraph Marks When Creating Ranges
How to: Extend Ranges in Documents
How to: Reset Ranges in Word Documents
Note