How to get Word to automatically fill the Edit > Find and Edit > Replace boxes with the selected text
Copy the macro on this page into your Normal template or an add-in file
Word will then default the 'find' text to your selected text.
When you do Edit > Find or Edit > Replace, Word remembers the text you went to 'Find' last time.
This is almost never what I want.
I'd like Word to default the 'Find' text to the text I've selected in the document.
So, I wrote two little macros to do the job.
These macros are not necessary in Word 2010. Word 2010 does what I'd always hoped for: the default text to search for is the selected text, not the last text I searched for.
How to use the macros
Just do Edit > Find (or ctrl-f), or Edit > Replace (or ctrl-h) as usual. These macros replace the existing commands, so they kick in automatically.
The text you select will be automatically inserted into the 'Find' text box on the Edit > Find or Edit > Replace dialog boxes, as shown below:
Figure 1: When you use these macros, Word will automatically default the 'Find' text to the text you have selected in the document.
If you select text in a numbered paragraph, copy it, and paste it into the Edit > Find or Edit > Replace dialogs, Word pastes the heading numbering along with the text.
That's very annoying.
Using these macros avoids that problem, because it just pastes the text—not the numbering!
How the macros work
The macros intercept the existing commands as described at Intercepting events like Save and Print.
Beware the trailing space!
If you double-click a word, then Word automatically selects the space after the word, if there is one. Like this:
Figure 2: When you double-click to select a Word, word automatically selects the trailing space.
These macros trim that trailing space. If you use these macros, the default 'Find' text will be "brown", not "brown ". If you need to search for "brown ", then you'll need to add the extra space.
How to install the macros
1. Look at the following for information on how to use the macros:
- Graham Mayor’s Idiots' Guide to Installing Macros
- What do I do with macros sent to me by other newsgroup readers to help me out?
2. Copy and paste the code of the macros at the end of this page.
Text of the macros
Here is the code you need to copy into your file:
Public Sub EditFind() 'Shauna Kelly, www.ShaunaKelly.com, 2006. 'A replacement for the Edit > Find dialog that 'defaults the 'Find' text to the currently selected 'text. On Error Resume Next With Dialogs(wdDialogEditFind) If Not Selection.Start = Selection.End Then 'If the user has selected some text, use that 'text as the default for the Edit > Find 'dialog box. .Find = Trim(Selection.Text) End If 'And show the Edit > Find dialog box as normal .Show End With End Sub Public Sub EditReplace() 'Shauna Kelly, www.ShaunaKelly.com, 2006. 'A replacement for the Edit > Find dialog that 'defaults the 'Find' text to the currently selected 'text. On Error Resume Next With Dialogs(wdDialogEditReplace) If Not Selection.Start = Selection.End Then 'If the user has selected some text, use that 'text as the default for the Edit > Replace 'dialog box .Find = Trim(Selection.Text) End If 'And show the Edit > Replace dialog box as normal .Show End With End Sub