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: Newest
  1. Anonymous
    2015-08-18T10:09:37+00:00

    Hi Rhonda, thanks a lot for your answer.

    I fixed the problem. It works now!  I had to change

    .MatchWildcards = True

    into

    .MatchWildcards = False

    to make the macro read the table correctly. I also added a red color to the replacements:

    .Replacement.Font.Color = wdColorRed

    So if you to use the macro for replacing words or phrases, use this code below:

    Sub ReplaceFromTableList()

    ' from Doug Robbins, Word MVP, Microsoft forums, Feb 2015, based on another macro written by Graham Mayor, Aug 2010

     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\your.name\Documents\Macros\FindReplaceTable.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

         Selection.HomeKey wdStory

         With oRng.Find

                 .ClearFormatting

                 .Replacement.ClearFormatting

                 .Replacement.Font.Color = wdColorRed

                 .MatchWildcards = False

                 .MatchWholeWord = True

                 .Text = rFindText.Text

                 .Replacement.Text = rReplacement.Text

                 .Forward = True

                 .Wrap = wdFindContinue

                 .Execute Replace:=wdReplaceAll

           End With

     Next I

     oChanges.Close wdDoNotSaveChanges

    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-08-18T09:16:41+00:00

    I wrote up what I did (acknowledging the help from this forum, of course) in this blog post: https://cybertext.wordpress.com/2015/03/03/word-macro-to-run-multiple-wildcard-find-and-replace-routines/ You might want to compare your code with the code that works for me.

    It works well for me (except for some occasional weird things with replacing an 'x' within a word (i.e. NOT surrounded by spaces) with a multiplication sign. 

    I have well over 120 F&R routines for non-breaking spaces in my table, and regularly run it over 50 to 400p Word documents. It takes only a couple of minutes. That said, I've only used it in Word 2010, not Word 2013. If I get a chance tomorrow, I'll test it in Word 2013 too and report back here.

    --Rhonda

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-08-18T08:38:31+00:00

    unfortunately it does not work in all cases for me (Word 2013). In tests with small documents it runs well, in documents with > 500 words and more replacements Word freezes (no response) and i have to kill it manually.

    I am using a list of around 50 replacements (in a table).

    Can anyone tell me what is wrong with my code below?

    Sub ReplaceFromTableList()

        Selection.WholeStory

        Selection.LanguageID = wdDutch

        Application.CheckLanguage = False

    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\Tidde\Documents\Macros\FRtable.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, _

                    MatchWholeWord:=True, _

                    MatchWildcards:=False, _

                    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-26T10:06:26+00:00

    Sorry, that was a typo.

    Was this answer helpful?

    0 comments No comments