Working with Bookmarks
Bookmarks provide you a way to name and keep track of a particular Range. The user can even edit the Range and the modified Range will still be accessible by its name unless the user completely deletes the Range.To create and manage bookmarks, you can use Word's Bookmark dialog. You can select some text in the document, choose Bookmark from the Insert menu, and give the range of text a name then click the Add button to add a bookmark, as shown in Figure 8-25. Existing bookmarks can be selected and navigated to using the Go To button. They can also be removed using the Delete button.
Figure 8-25. The Bookmark dialog.

Figure 8-26. Result of checking the Bookmarks check box in the View page of Word's Options dialog.

Listing 8-40. A VSTO Customization That Works with Bookmarks
Bookmarks are easily deleted from the document. For example, setting the Text property of the Range associated with a bookmark replaces the Range and in the process deletes the bookmark associated with the Range. VSTO extends Bookmark and adds some additional functionality to preserve the bookmark even when you set the Text property. For more information on VSTO's support for bookmarks and the bookmark control, see Chapter 13.
private void ThisDocument_Startup(object sender, EventArgs e)
{
object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
Word.Range r = Range(ref missing, ref missing);
r.Text = "The quick brown fox ";
object range1 = r;
this.Bookmarks.Add("FirstHalf", ref range1);
r.Collapse(ref collapseEnd);
r.Text = "jumped over the lazy dog.";
object range2 = r;
this.Bookmarks.Add("SecondHalf", ref range2);
if (this.Bookmarks.Exists("FirstHalf") == true)
{
MessageBox.Show("FirstHalf exists");
}
object firstHalfName = "FirstHalf";
Word.Bookmark b = this.Bookmarks.get_Item(ref firstHalfName);
MessageBox.Show(String.Format(
"FirstHalf starts at {0} and ends at {1}.",
b.Range.Start, b.Range.End));
}