VBA Find and Replace

link - wordmvp.com/FAQs/General/UsingWildcards.htm
link - gregmaxey.com/word_tip_pages/words_fickle_vba_find_property.html


Find Object

The Find property returns a Find object that you can use to Search a Range.


Selection.Find 
ActiveDocument.Content.Find

Properties and Methods

Font 
ClearFormatting 
FormatWhether to find formatting in addition to or instead of the Find text ??
Replacement 
Execute 
Forward 
Found 
Frame 
Highlight 
MatchCase 
MatchWholeWord 
MatchWildcards 
NoProofing 
ParagraphFormat 
Style 
Text 
Wrap 

Find.Execute

expression.Execute(FindText:="text to find", _ 
                   MatchCase:=, _
                   MatchWholeWord:=, _
                   MatchWildcards:=, _
                   MatchSoundsLike:=, _
                   MatchAllWordForms:=, _
                   Forward:=, _
                   Wrap:=wdFindWrap.wdFindContinue, _
                   Format:=, _
                   ReplaceWith:=, _
                   Replace:=, _
                   MatchKashida:=, _
                   MatchDiacritics:=, _
                   MatchAlefHamza:=, _
                   MatchControl:=)

FindText - Optional Variant. The text to be searched for. Use an empty string ("") to search for formatting only. You can search for special characters by specifying appropriate character codes. For example, "^p" corresponds to a paragraph mark and "^t" corresponds to a tab character. For a list of special characters you can use, see Find and replace text or other items .
MatchCase - Optional Variant. True to specify that the find text be case sensitive. Corresponds to the Match case check box in the Find and Replace dialog box (Edit menu).
MatchWholeWord - Optional Variant. True to have the find operation locate only entire words, not text that's part of a larger word. Corresponds to the Find whole words only check box in the Find and Replace dialog box.
MatchWildcards - Optional Variant. True to have the find text be a special search operator. Corresponds to the Use wildcards check box in the Find and Replace dialog box.
MatchSoundsLike - Optional Variant. True to have the find operation locate words that sound similar to the find text. Corresponds to the Sounds like check box in the Find and Replace dialog box.
MatchAllWordForms - Optional Variant. True to have the find operation locate all forms of the find text (for example, "sit" locates "sitting" and "sat"). Corresponds to the Find all word forms check box in the Find and Replace dialog box.
Forward - Optional Variant. True to search forward (toward the end of the document).



Iterating through all occurrences

You can use the Find object's Found property to iterate through several found items instead of checking the return value of Execute everytime.

Dim objFind As Word.Find 
   objFind = objRange.Find
   objFind.Text = "better"
   objFind.MatchWholeWord = True
   objFind.Execute
   While objFind.Found
      objFind.Execute
   End While

Looping through paragraphs in a document is very slow
If you are looking for certain text always use Range.Find


Replacing HTML tags

If you want to find and replace special characters you may need to prefix them with a back slash

objFind.Text = "\<b\>" 

When the Execute method of the Find object is executed successfully a new Range object is returned


Set objRange = ActiveDocument.Sentences(2) 
objRange.Find.Text = "some text"
objRange.Find.Execute

If (objRange.Find.Found = True) Then
   objRange.Select
End If

The following example finds the next double-spaced paragraph after the selection.

With Selection.Find 
    .ClearFormatting
    .ParagraphFormat.LineSpacingRule = wdLineSpacing.wdLineSpaceDouble
    .Text = ""
    .Forward = True
    .Wrap = wdFindWrap.wdFindContinue
End With
Selection.Find.Execute

This example finds all double-spaced paragraphs in the active document and replaces the formatting with 1.5-line spacing.

With ActiveDocument.Content.Find 
    .ClearFormatting
    .ParagraphFormat.Space2
    .Replacement.ClearFormatting
    .Replacement.ParagraphFormat.Space15
    .Execute FindText:="", _
             ReplaceWith:="", _
             Replace:=wdReplace.wdReplaceAll
End With

This example steps through the words in myRange (which spans from the beginning of the active document to the end of the selection) and deletes the word "BetterSolutions" (including the trailing space) wherever it occurs in the range.

Set myRange = ActiveDocument.Range(Start:=0, End:=Selection.End) 
For Each aWord In myRange.Words
    If aWord.Text = "BetterSolutions" Then
      aWord.Delete
   End If
Next aWord


Finding

Selection.Find.ClearFormatting ?? 
With Selection.Find
   .Text = "text_to_find"
   .Replacement.Text = "replace_with_text"
   .Forward = True
   .Wrap = wdFindWrap.wdFindContinue
   .Format = False
   .MatchCase = False
   .MatchWholeWord = False
   .MatchWildcards = False
   .MatchSoundsLike = False
   .MatchAllWordForms = False
End With

If Selection.Find.Execute = True Then
End If


Public Sub DoFindReplace(ByVal sFindText As String, _ 
                         ByVal sReplaceText As String)
   With Selection.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = sFindText
      .Forward = True
      .Wrap = wdFindWrap.wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcard = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
   End With

   Do While Selection.Find.Execute
'keep going until nothing is found
      .Execute Replace:=wdReplace.wdReplaceAll
   Loop
'free up some memory
   ActiveDocument.UndoClear
End Sub

Finding field codes

Selection.Find.ClearFormatting 
Selection.Find.Text = "^b" - this is a section break
Selection.Find.Text = "^d"
Selection.Find.Text = "^p" - this is a paragraph mark
Selection.Find.Replacement.ClearFormatting
Selection.Find.Execute Replace:=wdReplace.wdReplaceAll
Selection.Find.Text = "^L"
Selection.TypeText Text:="some text"
Selection.Range.InsertAutoText

Finding symbols

The first thing you need to do is find out both the font and Unicode number of the character you want to search for and /or replace with. If the character was inserted from the Insert + Symbol dialog, its name won't be displayed in the Fonts list on your toolbar, but you can get at the information by selecting the character and running the following macro:


Sub GetCharNoAndFont()


With Dialogs(wdDialogInsertSymbol)
Debug.Print "Font: " & .Font
Debug.Print "Char number " & .CharNum
End With


End Sub


Press Ctrl+G or select View + Immediate Window to see the results.


In the case of the Delta symbol, that will return:


Font: Symbol
Char number -3996


You can now use the following macros to find the next instance of the Delta symbol (if there is one). As you'll see shortly, it is very straightforward, even if you are a complete programming novice, to customise the following macros for your needs.


Sub FindDeltaSymbols()
'Call the main "FindSymbols" macro (below),
'and tell it what character code and font to search for
Call FindSymbols(FindChar:=ChrW(-3996), FindFont:="Symbol")
End Sub


Sub FindSymbols(FindChar As String, FindFont As String)


Dim FoundFont As String, OriginalRange As Range, strFound As Boolean
Application.ScreenUpdating = False


'set range to return to in case symbol not found
Set OriginalRange = Selection.Range


strFound = False
With Selection.Find
.ClearFormatting
.Text = FindChar
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

Do While .Execute 'Keep going until nothing found
If Dialogs(wdDialogInsertSymbol).Font = FindFont Then
'If the correct character was found, exit loop
strFound = True
Exit Do
Else
'Otherwise search again
Selection.Collapse wdCollapseEnd
End If
Loop


If Not strFound Then
'if nothing found, search from the beginning of the document
ActiveDocument.Range(0, 0).Select
Do While .Execute
If Dialogs(wdDialogInsertSymbol).Font = FindFont Then
strFound = True
Exit Do
Else
Selection.Collapse wdCollapseEnd
End If
Loop
End If

End With


If Not strFound Then
OriginalRange.Select
End If


Set OriginalRange = Nothing
Application.ScreenUpdating = True


End Sub


To customise it, all you have to do is to use the GetCharNoAndFont() macro again, to find out what chaacter number and font you need to use; and then change the character number and font used in the FindDeltaSymbols() macro to the correct values for the symbol(s) you want to find.


For example, if you want to find the one-eighth symbol, which is listed under "(normal text)" in the insert symbol dialog, insert the symbol into your document, select it, and run the GetCharNoAndFont() macro. This will return:


Font: (normal text)
Char number 8539


So just by changing the reference to "Symbol" in the FindDeltaSymbols() macro to "(normal text)" instead; and the reference to "-3996" to "8539"; we get the following:


Sub FindOneEigthSymbols()
Call FindSymbols(FindChar:=ChrW(8539), FindFont:= "(normal text)")
End Sub


Use this with exactly the same FindSymbols() macro as before.


Finding and replacing symbols
If you want to do a Find and Replace operation, you will need the character numbers and fonts for both the Find and the Replace characters. As before, you can use the GetCharNoAndFont() macro to obtain this information.


You can then customise the following first of the following macros, which calls the second macro, and in this example replaces all instances of the Delta symbol in your document with Beta symbols. To customise it, just change the character numbers and fonts in the ReplaceAllDeltaSymbolsWithBetaSymbols() macro (but leave the ReplaceAllSymbols() macro as it is):


Sub ReplaceAllDeltaSymbolsWithBetaSymbols()
'Call the main "ReplaceAllSymbols" macro (below),
'and tell it which character code and font to search for, and which to replace with
Call ReplaceAllSymbols(FindChar:= ChrW(-3996), FindFont:= "Symbol", _
ReplaceChar:=-3998, ReplaceFont:="Symbol")
End Sub


Sub ReplaceAllSymbols(FindChar As String, FindFont As String, _
ReplaceChar As String, ReplaceFont As String)


Dim FoundFont As String, OriginalRange As Range, strFound As Boolean
Application.ScreenUpdating = False


Set OriginalRange = Selection.Range
'start at beginning of document
ActiveDocument.Range(0, 0).Select


strFound = False
With Selection.Find
.ClearFormatting
.Text = FindChar
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

Do While .Execute
'keep searching until nothing found
If Dialogs(wdDialogInsertSymbol).Font = FindFont Then
'Insert the replacement symbol where the found symbol was
Selection.InsertSymbol Font:=ReplaceFont, _
CharacterNumber:=ReplaceChar, Unicode:=True
Else
Selection.Collapse wdCollapseEnd
End If
Loop

End With


OriginalRange.Select


Set OriginalRange = Nothing
Application.ScreenUpdating = True


End Sub





© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext