Selection Change

The SelectionChange event handler enables you to take action when the selection of a bookmark's content changes. For example, you might want to display information in the status bar about the characters that are currently selected. In this section, you'll add code to the Selection-Change event handler of the bookmark.

1. Create a Word document solution.

2. Add a bookmark to the document, and name it Bookmark1.

3. Add the code in Listing 6.26 to the SelectionChange event handler of the Bookmark control.

Listing 6.26. Displaying the characters in a bookmark up to the current selection

Sub Bookmark1_SelectionChange(ByVal sender As Object, _ ByVal e As Microsoft.Office.Tools.Word. _ SelectionEventArgs) Handles Bookmark1.SelectionChange

Dim mySelection As Word.Selection = Application.Selection Dim selectedCharacters As String = mySelection.Text Application.StatusBar = "Characters selected: " & _ selectedCharacters

End Sub

4. Press F5 to run the code.

5. Move the mouse cursor into the text of a bookmark, and watch how the status bar displays updated information about which characters are selected as you change the selection within a Bookmark control. Figure 6.19 illustrates.

Figure 6.19. Displaying selected text in the status bar
0 0

Post a comment