Compartilhar via


Bookmark.Collapse(Object) Method

Definition

Collapses a Bookmark control to the starting or ending position.

public void Collapse (ref object direction);
abstract member Collapse : obj -> unit
Public Sub Collapse (Optional ByRef direction As Object)

Parameters

direction
Object

The direction in which to collapse the Bookmark. Can be either of the following Microsoft.Office.Interop.Word.WdCollapseDirection constants: wdCollapseEnd or wdCollapseStart. The default value is wdCollapseStart.

Examples

The following code example adds a Bookmark control with text, and then displays the start and end position of the bookmark before and after collapsing the bookmark.

This example is for a document-level customization.

private void BookmarkCollapse()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();
    Microsoft.Office.Tools.Word.Bookmark bookmark1 =
        this.Controls.AddBookmark(this.Paragraphs[1].Range,
        "bookmark1");

    bookmark1.Text = "This is sample bookmark text.";
    MessageBox.Show("Start and end of bookmark1 before calling Collapse: " +
        bookmark1.Start.ToString() + " and " + bookmark1.End.ToString());

    object Direction = Word.WdCollapseDirection.wdCollapseEnd;
    bookmark1.Collapse(ref Direction);

    MessageBox.Show("Start and end of bookmark1 after calling Collapse: " +
        bookmark1.Start.ToString() + " and " + bookmark1.End.ToString());
}
Private Sub BookmarkCollapse()

    Me.Paragraphs(1).Range.InsertParagraphBefore()
    Dim Bookmark1 As Microsoft.Office.Tools.Word.Bookmark = _
        Me.Controls.AddBookmark(Me.Paragraphs(1).Range, "Bookmark1")

    Bookmark1.Text = "This is sample bookmark text."
    MessageBox.Show("Start and end of Bookmark1 before calling Collapse: " _
        & Bookmark1.Start.ToString() & " and " & Bookmark1.End.ToString)

    Dim Direction As Object = Word.WdCollapseDirection.wdCollapseEnd
    Bookmark1.Collapse(Direction)

    MessageBox.Show("Start and end of Bookmark1 after calling Collapse: " _
        & Bookmark1.Start.ToString() & " and " & Bookmark1.End.ToString)

End Sub

Remarks

After a Bookmark control is collapsed, the starting and ending points are equal.

If you use wdCollapseEnd to collapse a Bookmark control that refers to an entire paragraph, the range is located after the ending paragraph mark (the beginning of the next paragraph). However, you can move the range back one character by using the Microsoft.Office.Interop.Word.Range.MoveEnd* method after the range of the Bookmark control is collapsed.

Optional Parameters

For information on optional parameters, see Optional Parameters in Office Solutions.

Applies to