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: Most helpful
  1. 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
  2. Anonymous
    2015-02-26T09:12:29+00:00

    Final update: Once I made those same changes in the macros.dotm in the STARTUP folder, it worked with all documents with no errors.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-02-26T08:11:19+00:00

    Thanks, Doug! About to try it now.

    However, when I pasted the code into the VBA code window, I got red (error) lines for the Forward, Wrap, and Execute lines. When I removed the ':' in each of these lines and added a space before and after the '=', the red went away.

    But when I tried to run it on a test document, I got yet another error message: "Compile error in hidden module: NewMacros. This error commonly occurs when code is incompatible with the version, platform, or architecture of this application. Click 'Help' for information on how to correct this error." The Help indicated it might be a 32-bit doc trying to work with a 64-bit Office, but even though I have a 64-bit machine, I installed 32-bit Office on it. 

    I'll keep persevering and see if I can recreate my Macros.dotm document in my current version of Word (I think I created it in Word 2007).

    I'll let you know how I go.

    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-26T07:45:19+00:00

    Try this:

    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

         Selection.HomeKey wdStorey

         With oRng.Find

                 .ClearFormatting

                 .Replacement.ClearFormatting

                 .MatchWildcards:=True

                 .Text = rFindText.Text

                 .Replacement.Text = rReplacement.Text

                 .Forward:=True

                 .Wrap:=wdFindContinue

                 .Execute Replace:=wdReplaceAll

           End With

     Next i

     oChanges.Close wdDoNotSaveChanges

    Was this answer helpful?

    0 comments No comments