macro for performing more than one find and replace procedure at the same time

Anonymous
2010-08-07T18:21:24+00:00

ok, i've got to about 700!! find and replace procedures for a document of TWO MILLION words so this macro could save me about 4 hours of work.

i tried to rewrite this macro so that it would work but it didn't.  can anyone help me out

Sub Macro1()

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " mit der "
.Replacement.Text = " mitder"
.Text = " mit dem "
.Replacement.Text = " mitdem"
.Text = " mit den "
.Replacement.Text = " mitden"
.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

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Anonymous
2010-08-07T18:56:51+00:00

ok, i got it work, i just copied it over

Sub Macro1()

'

' Macro1 Macro

' Macro recorded 8/7/10 by Seely Foley

'

    Selection.Find.ClearFormatting

    Selection.Find.Replacement.ClearFormatting

    With Selection.Find

        .Text = " mit der "

        .Replacement.Text = " mitder"

        .Forward = True

        .Wrap = wdFindContinue

        .Format = False

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

    Selection.Find.Execute Replace:=wdReplaceAll

    Selection.Find.ClearFormatting

    Selection.Find.Replacement.ClearFormatting

    With Selection.Find

        .Text = " mit den "

        .Replacement.Text = " mitden"

        .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

Was this answer helpful?

30+ people found this answer helpful.
0 comments No comments

53 additional answers

Sort by: Oldest
  1. Anonymous
    2015-02-25T08:59:42+00:00

    I realise this is a very old thread, but I needed a multiple find and replace (F&R) macro that dealt with 100+ wildcard F&R routines. This macro appears to work very well, but perhaps too well in my case. In case it matters, I'm using Word 2010.

    I created a doc with a single 2-column table with 'find' routines in the left column and 'replace' routines in the right. I used the wildcard syntax as I run all these as wildcard F&R routines. For example, I had a routine to search for a number followed by a space followed by a month name--in wildcard speak that's something like ([0-9])( )(May). I wanted to replace it with the same text except for the space, which I wanted as a non-breaking space; i.e. \1^s\3. 

    I changed three lines in the macro -- the one that pointed to my Word doc containing the table, the 'MatchWholeWord' to False, and the 'MatchWildcards' to True. I changed nothing else.

    I added this slightly revised macro to a macros.dotm doc I store in my Word>STARTUP folder. And then I opened a large doc and ran the macro on it. After several minutes, it was finished. However, everything that was searched for was replaced by the ACTUAL text in the 'replace' column, and Word didn't consider it as wildcard syntax.

    The macro correctly searched for all instances in the first column, using the wildcard syntax. But the text it replaced it with ignored the wildcard setting and went in as \1^s\3. So instead of 9 May being replaced with 9<non-breaking space>May, it got replaced with \1^s\3.

    I also tried putting parentheses around the elements in the replace syntax--i.e. ( \1)(^s)(\3)--but again, it just went in as that text, and not as I would've expected had I done a Ctrl+H F&R routine with wildcards checked.

    Can anyone help? What line(s) do I need to add/change/delete in the macro for it to read the contents of the 'replace' column as wildcard syntax, and not as text?

    Thanks!

    --Rhonda

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323.6K Reputation points MVP Volunteer Moderator
    2015-02-25T09:35:39+00:00

    It will be easier to help you if you show us the code of the macro that you are using.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-02-25T22:14:42+00:00

    Hi Doug

    Here is the code:

    Sub ReplaceFromTableList()

    Dim oChanges As Document, oDoc As Document

    Dim oTable As Table

    Dim oRng As Range

    Dim rFindText As Range, rReplacement As Range

    Dim i As Long

    Dim sFname As String

    'Change the path in the line below to reflect the name and path of the table document

    sFname = "C:\Users\rhonda\AppData\Roaming\Microsoft\Word\STARTUP\find_and_replace_routines_macro.docx"

    Set oDoc = ActiveDocument

    Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)

    Set oTable = oChanges.Tables(1)

    For i = 1 To oTable.Rows.Count

        Set oRng = oDoc.Range

        Set rFindText = oTable.Cell(i, 1).Range

        rFindText.End = rFindText.End - 1

        Set rReplacement = oTable.Cell(i, 2).Range

        rReplacement.End = rReplacement.End - 1

        With oRng.Find

                .ClearFormatting

                .Replacement.ClearFormatting

                Do While .Execute(findText:=rFindText, _

                    MatchWildcards:=True, _

                    Forward:=True, _

                    Wrap:=wdFindContinue) = True

                    oRng.Text = rReplacement

                Loop

        End With

    Next i

    oChanges.Close wdDoNotSaveChanges

    End Sub

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.6K Reputation points MVP Volunteer Moderator
    2015-02-26T02:05:17+00:00

    Your code is just replacing the found text with the text contained in the replacement range.

    Using the following should make the replacement properly

    Sub ReplaceFromTableList()

     Dim oChanges As Document, oDoc As Document

     Dim oTable As Table

     Dim oRng As Range

     Dim rFindText As Range, rReplacement As Range

     Dim i As Long

     Dim sFname As String

     'Change the path in the line below to reflect the name and path of the table document

     sFname = "C:\Users\rhonda\AppData\Roaming\Microsoft\Word\STARTUP\find_and_replace_routines_macro.docx"

     Set oDoc = ActiveDocument

     Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)

     Set oTable = oChanges.Tables(1)

     For i = 1 To oTable.Rows.Count

         Set oRng = oDoc.Range

         Set rFindText = oTable.Cell(i, 1).Range

         rFindText.End = rFindText.End - 1

         Set rReplacement = oTable.Cell(i, 2).Range

         rReplacement.End = rReplacement.End - 1

         With oRng.Find

                 .ClearFormatting

                 .Replacement.ClearFormatting

                 Do While .Execute(findText:=rFindText, _

                     MatchWildcards:=True, _

                     Forward:=True, _

                     Wrap:=wdFindContinue, Replacewith:=rReplacement) = True

                 Loop

         End With

     Next i

     oChanges.Close wdDoNotSaveChanges

    Was this answer helpful?

    0 comments No comments