The Microsoft Word Find and Replace feature is very powerful and a great time saver for the more skilled user. You can use Find and Replace to locate exact words, phrases and even patterns matching various scenarios.
Word Find and Replace
Let us start with exploring how to do a regular Find and Replace in Word.
Click the Find or Replace buttons in the Home ribbon Editing section
- If you want to Find click Find
- If you want to Find and Replace click Replace
- Match case – will only find words/sentences that match the letter case (e.g. A vs a)
- Find whole words only – will only find whole words (if looking for “ate” will only match ” ate “ and not “late”)
- Use wildcards – allows you to use wildcards (click the Special button for list of wildcard special characters that can be used
- Sounds like – matches expressions that sound like provided text
- Find all word forms – matches all words/sentences that match a word form (e.g. “doyle” will also match “doyl” as it sounds similar)
- Match prefix – match text matching a prefix of a word
- Match suffix – match text matching a suffixof a word
- Ignore punctuation characters – will ignore punctionation
- Ignore white-space characters – will ignore white-space (” “)
This will open the Find and Replace window.
Provide a word, sentence and/or wildcard special characters
Provide a word/sentence you want to Find in the Find what text field and the word/sentence you want to replace it with in the Replace with text field.
Below and explanation of key buttons used to Find or Replace text:
Although Find and Replace is a basic and very easy to use function it is often underestimated. Especially that many users do not know that you can easily use wildcards to replace more complex text patterns.
You can also you wildcards to replace various complex patterns such as sequences of numbers or specific number of occurances, letter cases, characters use to replace any characters and much more. To use wilcards click More > > and select the Use wilcards checkbox.
On the right you should see all available wildcard characters.
Let us explore some example common scenarios below:
Match any word made of A-Z characters, any letter case
This matches any single word that contains A-z letters.
The < character indicate the beginning, while > the end of a word. The [A-z] brackets indicate a series of characters, using the hyphen allows you specify the whole range of A-z letters. Lastly the @ character indicates that the previous expression may repeat 0 to any number of times.
Match an email from the .com domain
This matches only emails with A-z letters and 0-9 numbers in their login and domain name. Again the [A-z,0-9] bracket specifies we are listing several ranges of acceptable characters, following this with the @ characters tells that any number of these characters may appear. To use the @ character explicitly we need to escape it with a backslash \. We use the similar patter for the domain name. Finally notice again I am using < and > to indicate the beginning or end of a word as emails are not separated by spaces.
Match a phone number split with hyphens
The above matches any 3 series of digits separated by hyphens.
Using Wildcards to Capture and Replace text
In some cases you will want to not only capture a pattern but replace it with part of its content. For this you need to use Expressions (). Expressions let you mark a specific group in the “Find what” text field, that you want to reuse in your “Replace with” text field. Below a simple example:
Example: Switch places of 2 numbers
In this example we have a pattern of numbers separated by hyphens. Let us assume we want to switch places of these two 3-digit numbers.
Some text 123-456, some other text 789-012. Something else 345-678
The resulting Text:
Some text 456-123, some other text 012-789. Something else 678-345
Example: Replace Email domain
Imagine you want to replace an email domain from yahoo to gmail on all emails in your Word document. If you didn’t know Expressions you would use wildcards to find a match an manually replace all such cases. However below an example that will replace this automatically:
All Expressions () are numbered by the sequence in which they are used. This allows us to reference the first part of the email by using the backslash and number \1.
VBA Find and Replace
You can also execute a Find and Replace sequence using a VBA Macro:
Find a single match
The below procedure will print out all occurances of “Find Me” phrases.
ActiveDocument.Content.Select With Selection.Find .Text = "Find Me" .Forward = True .Execute End With If Selection.Find.Found Then Debug.Print "Found: " & Selection.Range 'Print the found match Else Debug.Print "Not Found" End If
Find all matches
Below VBA macro will find all emails in a Word document with their mailto hyperlinks. This is a good example of fixing hyperlinks in Word documents.
ActiveDocument.Content.Select Do With Selection.Find .Text = "<[A-z,0-9]@\@[A-z,0-9]@\.com>" .MatchWildcards = True .Forward = True .Execute End With If Selection.Find.Found Then ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="mailto:" & Selection.Range Else Exit Do 'If not found then end the loop End If Loop
Here are my main takeaways from using Find and Replace in Microsoft Word