none
multi-step find and replace in Word? RRS feed

  • Question

  • I would like to replace text in a text portion with corresponding adjacent text in a table. Example:

    Text:  200 is the best thing ever. But 201 is even better than that.

    Table 1

    200

    BEST THING 200

    201

    BETTER THING 201

    Revised text: BEST THING 200 is the best thing ever. But BETTER THING 201 is even better than that.

    I would like to do this just once for the entire document, because my table entries can get very long, and I don't want to do "find and replace" for each separate text/text portion

    Any suggestions on how to proceed?

    Thanks.

    Sunday, May 20, 2012 3:46 PM

Answers

  • Assuming the Find/Replace table is in a separate document, you could use a macro like:

    Sub BulkFindReplace()
    Application.ScreenUpdating = False
    Dim ThisDoc As Document, FRDoc As Document, Rng As Range, i As Long
    Set ThisDoc = ActiveDocument
    Set FRDoc = Documents.Open("Drive:\FilePath\FindReplaceList.doc", _
      Visible:=False, AddToRecentFiles:=False)
    With ThisDoc.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .MatchWholeWord = True
      .MatchCase = True
       'Process each word from the F/R Table
      For i = 1 To FRDoc.Tables(1).Rows.Count
        Set Rng = FRDoc.Tables(1).Rows(i).Cells(1).Range
        Rng.End = Rng.End - 1
        .Text = Rng.Text
        Set Rng = FRDoc.Tables(1).Rows(i).Cells(2).Range
        Rng.End = Rng.End - 1
        .Replacement.Text = Rng.Text
        .Execute Replace:=wdReplaceAll
      Next
    End With
    FRDoc.Close False
    Set Rng = Nothing: Set FRDoc = Nothing: Set ThisDoc = Nothing
    Application.ScreenUpdating = True
    End Sub

    Simply supply the correct path & name for the document containing the Find/Replace table.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, May 21, 2012 12:05 AM

All replies

  • Assuming the Find/Replace table is in a separate document, you could use a macro like:

    Sub BulkFindReplace()
    Application.ScreenUpdating = False
    Dim ThisDoc As Document, FRDoc As Document, Rng As Range, i As Long
    Set ThisDoc = ActiveDocument
    Set FRDoc = Documents.Open("Drive:\FilePath\FindReplaceList.doc", _
      Visible:=False, AddToRecentFiles:=False)
    With ThisDoc.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .MatchWholeWord = True
      .MatchCase = True
       'Process each word from the F/R Table
      For i = 1 To FRDoc.Tables(1).Rows.Count
        Set Rng = FRDoc.Tables(1).Rows(i).Cells(1).Range
        Rng.End = Rng.End - 1
        .Text = Rng.Text
        Set Rng = FRDoc.Tables(1).Rows(i).Cells(2).Range
        Rng.End = Rng.End - 1
        .Replacement.Text = Rng.Text
        .Execute Replace:=wdReplaceAll
      Next
    End With
    FRDoc.Close False
    Set Rng = Nothing: Set FRDoc = Nothing: Set ThisDoc = Nothing
    Application.ScreenUpdating = True
    End Sub

    Simply supply the correct path & name for the document containing the Find/Replace table.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, May 21, 2012 12:05 AM
  • Hi, erichal, Check here to see how to quick find and replace word document in C#

     Regards.

    Tuesday, May 22, 2012 6:20 AM