If you really need to skip matching on Water (but want to catch all other case possibilities), I’d say you have two reasonable options.
1) Run multiple Replace All searches using wildcards to limit the case possibilities. For instance, the first wildcard search would be «w[Aa][Tt][Ee][Rr]» which would find any version of water that begins with a lowercase w. Then you could do «WA[Tt][Ee][Rr]» and so on. This could get laborious if you have a lot of words, but it would avoid the issue. You could also automate generating the wildcard searches, but that’s only worthwhile if you’re dealing with variable search text or a large number of words.
2) Don’t replace all; rather, loop through all the hits and decide if you want to make the replacement. Something like this:
Sub callback(control As IRibbonControl)
stringReplaced = stringReplaced + "string to be searched"
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
ActiveDocument.TrackRevisions = True
.Text = "wATer"
.Replacement.Text = "Water"
.Wrap = wdFindContinue
.MatchWholeWord = True
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
While .Execute .Forward = True and .Wrap = wdFindStop
if myStoryRange.Text <> "Water" then myStoryRange.Text = "Water"
myStoryRange.Collapse wdCollapseEnd
Wend
End With
ActiveDocument.TrackRevisions = False
Next myStoryRange
End Sub
Looping through searches can be tricky (see Greg Maxey’s exhaustive review of the process here: http://gregmaxey.mvps.org/word_tip_pages/words_fickle_vba_find_property.html
But with a little testing it could be a workable solution. Note of course that this will be slower than ReplaceAll.
(Please test that code before running it on anything real. I don’t have Word right now and am working a tiny bit from memory regarding setting up the loop.)
Word VBA Find
This example is a simple word macro find the text “a”:
Sub SimpleFind()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "a"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub
Find and Replace
This simple macro will search for the word “their” and replace it with “there”:
Sub SimpleReplace()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "their"
.Replacement.Text = "there"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Find and Replace Only in Selection
This VBA macro will find and replace text in a selection. It will also italicize the replaced text.
Sub ReplaceInSelection()
'replaces text JUST in selection . in adittion it makes replaced text italic
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "their"
With .Replacement
.Font.Italic = True
.Text = "there"
End With
.Forward = True
.Wrap = wdFindStop 'this prevents Word from continuing to the end of doc
.Format = True 'we want to replace formatting of text as well
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
This line of code prevents VBA from continuing to the end of the Word document:
.Wrap = wdFindStop 'this prevents Word from continuing to the end of doc
This line of code indicates to replace the formatting of the text as well:
.Format = True 'we want to replace formatting of text as well
Find and Replace Only In Range
Instead of replacing text throughout the entire document, or in a selection, we can tell VBA to find and replace only in range. In this example we defined the range as the first paragraph:
Dim oRange As Range
Set oRange = ActiveDocument.Paragraphs(1).Range
Sub ReplaceInRange()
'replaces text JUST in range [in this example just in the first paragraph]
Dim oRange As Range
Set oRange = ActiveDocument.Paragraphs(1).Range
oRange.Find.ClearFormatting
oRange.Find.Replacement.ClearFormatting
With oRange.Find
.Text = "their"
.Replacement.Text = "there"
.Forward = True
.Wrap = wdFindStop 'this prevent Word to continue to the end of doc
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
oRange.Find.Execute Replace:=wdReplaceAll
End Sub
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.
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 a word or sentence in your Word file go to the Home ribbon tab and go to the 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.
You can also use the CTRL+F keyboard shortcut to Find and the CTRL+H keyboard shortcut to do a Find and Replace.
If you click More > > you will see the full set of options below:
The following options are available:
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.
Using Wilcards
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.
For more information on Special Characters that can be used in Wildcard Find and Replace read this
Let us explore some example common scenarios below:
Match any word made of A-Z characters, any letter case
<[A-z]@>
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
<[A-z,0-9]@@[A-z,0-9]@.com>
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
[0-9]@-[0-9]@-[0-9]@
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.
Text:
Some text 123-456, some other text 789-012. Something else 345-678
Find what:
([0-9]{3})-([0-9]{3})
Replace with:
-
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
Conclusions
Here are my main takeaways from using Find and Replace in Microsoft Word