O'Reilly logo

Writing Word Macros, Second Edition by Steven Roman PhD

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required