Analyst Cave

Find and Replace in Word – Using with Wildcards and VBA

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 a word or sentence in your Word file go to the Home ribbon tab and go to the Editing section.

<[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:

\2-\1

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

Exit mobile version