Word 2004 Tip: Adding a ‘Find With Selection’ command to Word’s text searching features

Posted by Pierre Igot in: Macintosh
June 11th, 2004 • 4:01 am

Yesterday I posted a tip describing how to add a “Find Previous” command to Word’s text searching user interface. In the comments on this item, Evan asked about a “Use Selection for Find” command similar to the one available in other Mac OS X applications (such as Mail). I suggested that it was probably possible with a macro. I was going to try it later on, but Patrick Wynne beat me to it and was kind enough to submit his own WinWord macro.

I tested his macro in Word 2004 and it worked fine. Here it is:

Sub FindWithSelection()
'
' FindWithSelection Macro
' Macro created 11/06/04 by Patrick Wynne
'
    Dim seltext As String

    'get selection text without start/end spaces
    seltext = Trim(Selection.Text)

    'we want to start our search after current selection
    '(otherwise the first found instance will be the selected text!)
    Selection.Collapse wdCollapseEnd

    'now, find!
    With Selection.Find
        .ClearFormatting
        .Text = seltext
        .Forward = True
        .Wrap = wdFindStop 'don't wrap to top
        .Execute
    End With
End Sub

I called it “FindWithSelection” rather than “UseSelectionForFind” but that’s just for internal purposes when editing macro commands in Word. (I prefer it when similar commands appear together alphabetically in my list of macros.) You can always use the “Use Selection for Find” phrase when adding the command to Word’s “Edit” menu with “Customize…“.

Thanks Patrick!


Comments are closed.

Leave a Reply

Comments are closed.