Results 1 to 5 of 5
  1. #1
    4 Star Lounger
    Join Date
    Jun 2003
    Location
    Utah, USA
    Posts
    412
    Thanks
    39
    Thanked 5 Times in 5 Posts

    Word: Batch Find and Replace

    I'm using Word 2013 on Windows 8.1.

    Is there a way to set up something like a hash of items I'd like to find and replace that I could use on multiple documents?

    For example, I am scraping data off the internet into an HTML file. I need to print a nice copy of that, so I want to convert the HTML into Word to control the print.

    But some characters are lost in translation.

    Currently, my Word version shows:

    ’ when it should show '
    “ when it should show "

    and so on.

    I can go through this file and do all the find and replace work by hand.

    But then I'll have another HTML conversion that will need the same thing done to it.

    (I am aware that there are freeware programs out there to do this. But, our system administrators have locked down our machines pretty tightly this past year. So something inside Word would be easier in my situation thas something I have to install).

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    The following macro allows you to use a document holding numerous paragraphs of tab-separated Find/Replace strings as the source for a large-scale Find/Replace operation. One paragraph per Find/Replace is required, formatted as: Find text <Tab> Replace text. Empty paragraphs & paragraphs without tabs are not allowed.
    Code:
    Sub BulkFindReplace()
    Application.ScreenUpdating = False
    Dim FRDoc As Document, FRList, j As Long
     'Load the strings from the reference doc into a text string to be used as an array.
    Set FRDoc = Documents.Open("Drive:\FilePath\FindReplaceList.doc")
    FRList = FRDoc.Range.FormattedText
    FRDoc.Close False
    Set FRDoc = Nothing
    With ActiveDocument.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .MatchWholeWord = True
      .MatchCase = True
       'Process each word from the Check List. Tab-delimited strings are assumed, formatted as:
       'Find text <Tab> Replace text
      For j = 0 To UBound(Split(FRList, vbCr)) - 1
        .Text = Split(Split(FRList, vbCr)(j), vbTab)(0)
        .Replacement.Text = Split(Split(FRList, vbCr)(j), vbTab)(1)
        .Execute Replace:=wdReplaceAll
      Next
    End With
    Application.ScreenUpdating = True
    End Sub
    The following macro takes a similar approach, but uses a table for the Find/Replace strings instead. Note also that, in this case, the reference document remains open while the Find/Replace operation is undertaken.
    Code:
    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 
        Set Rng = FRDoc.Tables(1).Rows(i).Cells(2).Range
        Rng.End = Rng.End - 1
        .Replacement.Text = Rng
        .Execute Replace:=wdReplaceAll
      Next
    End With
    FRDoc.Close False
    Set Rng = Nothing: Set FRDoc = Nothing: Set ThisDoc = Nothing
    Application.ScreenUpdating = True
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. The Following 2 Users Say Thank You to macropod For This Useful Post:

    boobounder (2014-10-10),Charles Kenyon (2014-10-12)

  4. #3
    4 Star Lounger
    Join Date
    Jun 2003
    Location
    Utah, USA
    Posts
    412
    Thanks
    39
    Thanked 5 Times in 5 Posts
    I think the first one suits my purposes best.

    It's not quite working though.

    Following the examples in my first post, I created a Word document containing:

    €™ '
    €œ “

    Here's the first instance as an example of what it did.

    Before

    It€™s

    After

    It’
    €œs

    So it made the replace, but left behind different stuff (and yes, that's a carriage return in there).

    Suggestions?

  5. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Your output suggests you had a manual line break in the reference document instead of a paragraph break.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. The Following User Says Thank You to macropod For This Useful Post:

    boobounder (2014-10-10)

  7. #5
    4 Star Lounger
    Join Date
    Jun 2003
    Location
    Utah, USA
    Posts
    412
    Thanks
    39
    Thanked 5 Times in 5 Posts
    Bingo. Sorry about that. I just copied my example from the first post.

    I am good to go ... although I'm always interested in reading alternative solutions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •