Collapse Method
Collapse Method
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
Collapses a range or selection to the starting or ending position. After a range or selection is collapsed, the starting and ending points are equal.
expression.Collapse(Direction)
expression Required. An expression that returns one of the objects in the Applies To list.
PbCollapseDirection
PbCollapseDirection can be one of these PbCollapseDirection constants. |
pbCollapseEnd |
pbCollapseStart |
Remarks
If you use pbCollapseEnd to collapse a range that refers to an entire paragraph, the range will be located after the ending paragraph mark (the beginning of the next paragraph). However, you can move the range back one character by using the MoveEnd method after the range is collapsed.
Example
This example inserts text at the beginning of the second paragraph in the first shape on the first page of the active publication. This example assumes that the specified shape is a text frame and not another type of shape.
Sub CollapseRange()
Dim rngText As TextRange
Set rngText = ActiveDocument.Pages(1).Shapes(1) _
.TextFrame.TextRange
'Collapses range to the end of the range and
'enters new text and a new paragraph
With rngText
.Paragraphs(Start:=1, Length:=1).Collapse Direction:=pbCollapseEnd
.Text = "This is a new paragraph." & vbCrLf
End With
End Sub
This example places new text at the end of the first paragraph in the first shape on the first page of the active publication. This example assumes that the specified shape is a text frame and not another type of shape.
Sub CollapseSelection()
ActiveDocument.Pages(1).Shapes(1).TextFrame.TextRange _
.Paragraphs(Start:=1, Length:=1).Select
'Collapses selection to end and moves insertion point back
'one character, then enters new text
With Selection.TextRange
.Collapse Direction:=pbCollapseEnd
.MoveEnd Unit:=pbTextUnitCharacter, Size:=-1
.Text = " This is a new test."
End With
End Sub