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. Anonymous
    2015-08-19T02:01:17+00:00

    The macro evolving here makes several assumptions.  That is ok, so does my alteration to find and comment later in this thread.  Still I thought I would mention them just to save others from possibly scratching their heads in frustration later on.

    First it assumes, probably correctly, that the user wants to search the main text area of the document and fails to indicate or warn the user that the search and replace will affect only the main text area.  The point is that a simple VBA find and replace like this one only looks in the main text storyrange.

    It assumes that the file path list and it assumes that the file at that path has a table with at least two columns.  If any of those assumptions are wrong the code will puke.

    What follows is still a relatively basic VBA find and replace procedure but it does address the issue identified above.

    Sub ReplaceFromTableList()

    Dim oListDoc As Document, oDoc As Document

    Dim strFile_Path As String

    Dim lngType As Long

    Dim oTbl As Table

    Dim oRng As Range

    Dim strFind As String, strReplace As String

      'What to search

      Set oDoc = ActiveDocument

      'Where to search

      lngType = Selection.Range.StoryType

      'What to search for and replace with.  Change path below reflect

      "the name and path of the table document

      strFile_Path = "D:\List.docm"

      'Handle possible errors.

      On Error GoTo Err_Handler

      'Open list file.

      Set oListDoc = Documents.Open(FileName:=strFile_Path, Visible:=False)

      Set oTbl = oListDoc.Tables(1)

      For lngIndex = 1 To oTbl.Rows.Count

        'Set the search range. A simple VBA F&R does not search all storyranges.

        Set oRng = oDoc.StoryRanges(lngType)

        strFind = oTbl.Cell(lngIndex, 1).Range.Text

        strFind = Left(strFind, Len(strFind) - 2)

        strReplace = oTbl.Cell(lngIndex, 2).Range.Text

        strReplace = Left(strReplace, Len(strReplace) - 2)

        With oRng.Find

          .ClearFormatting

          .Replacement.ClearFormatting

          .MatchWildcards = False

          .MatchWholeWord = True

          .Text = strFind

          .Replacement.Text = strReplace

          .Forward = True

          .Wrap = wdFindContinue

          .Execute Replace:=wdReplaceAll

        End With

      Next lngIndex

      oListDoc.Close wdDoNotSaveChanges

    lbl_Exit:

      Exit Sub

    Err_Handler:

      Select Case Err.Number

        Case 5941:  MsgBox "The source table is missing or missing a required column."

        Case Else:  MsgBox Err.Description

      End Select

      Resume lbl_Exit

    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-08-18T23:50:34+00:00

    Sub FindAndCommentFromList()

    'A Word basic macro by Greg Maxey

    Dim oDocList As Document, oDoc As Document

    Dim oTbl As Table

    Dim oRng As Range

    Dim oRngText As Range, oRngComment As Range

    Dim lngIndex As Long

      Set oDoc = ActiveDocument

      Set oDocList = Documents.Open(FileName:="D:\List.docm", Visible:=False)

      Set oTbl = oDocList.Tables(1)

      For lngIndex = 1 To oTbl.Rows.Count

        Set oRng = oDoc.Range

        Set oRngText = oTbl.Cell(lngIndex, 1).Range

        oRngText.End = oRngText.End - 1

        Set oRngComment = oTbl.Cell(lngIndex, 2).Range

        oRngComment.End = oRngComment.End - 1

        With oRng.Find

          .ClearFormatting

          .MatchWildcards = False

          .MatchWholeWord = True

          .Text = oRngText.Text

          .Replacement.Text = oRngComment.Text

          .Forward = True

          .Wrap = wdFindStop

          While .Execute

            oRng.Comments.Add oRng, oRngComment.Text

            oRng.Collapse wdCollapseEnd

          Wend

        End With

      Next lngIndex

      oDocList.Close wdDoNotSaveChanges

    lbl_Exit:

      Exit Sub

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-08-18T23:17:23+00:00

    This macro has been incredibly helpful, especially when using the table as a database that can be constantly updated.

    Is there any way to change the code so that instead of finding and replacing, the macro finds the text in the first column and adds a new comment containing the text in the second column?

    I'm trying to use the macro for suggestions rather than hard changes, and sometimes there are multiple suggestions that vary depending on context and intent.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2015-08-18T11:51:48+00:00

    yes i tried your tool and it works well.

    however, i dont want to go trough dialogs and menus selecting options and files everytime i use it, because i have many files to treat. I didnt find a way to save settings.

    This VBA macro discussed here does the same thing by hitting just one key or button.

    Best

    Tidde

    Was this answer helpful?

    0 comments No comments