Example: Quick Selection

In this example, we create a simple but useful item selection program. The purpose of this program is to make selecting an item such as a word, sentence, line, paragraph, field, column, or table as easy as possible.

In particular, by pressing a hotkey (I use Alt-S), the dialog box in Figure 20-6 will appear. While this dialog box is showing, a single key (no need even to use the Alt key) will make a selection. For instance, striking the S key will select (highlight) the sentence that contains the insertion point.

Select an Item dialog box

Figure 20-6. Select an Item dialog box

The form is similar in design to the form used in the letter closing example, so I will leave the details to you. The entire code is listed in Example 20-7.

Example 20-7. Source Code for the Select an Item Dialog Box

Private Sub cmdCancel_Click()
   Unload Me
End Sub

Private Sub cmdOK_Click()
Dim fld As Field
If optItemCell Then
   Selection.Expand wdCell ElseIf optItemCharacter Then Selection.Expand wdCharacter ElseIf optItemColumn Then Selection.Expand wdColumn ElseIf optItemField Then For Each fld In ActiveDocument.Fields If Selection.Range.InRange(fld.Result) Then fld.Select Exit For End If Next ElseIf optItemLine Then Selection.Expand wdLine ElseIf optItemParagraph Then Selection.Expand wdParagraph ElseIf optItemRow Then Selection.Expand wdRow ElseIf optItemSection Then Selection.Expand wdSection ElseIf optItemSentence ...

Get Writing Word Macros, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.