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

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