Word macro: how to replace strings in a text, based on a 2 column-table?

5,191

Solution 1

I took the liberty of borrowing Raystafarian's snippet and expanding on it. You would change the line filename="c:\folder\file.txt with the path and filename of a plain text file, where the contents are a word or phrase, followed by a tab, followed by a word or phrase, followed by end of line. I didn't bother with error checking, so if you don't have an end of line on the last line, the text for that line will not be replaced. Also if you don't include a tab on each line, this macro will probably break. Back up your word document to facilitate easy reversion.

Sub Macro1()
    Dim ff As Long
    Dim x As Long
    Dim filename As String
    Dim buffer As String
    Dim charbuffer As String * 1

    filename = "c:\folder\file.txt"

    ff = FreeFile
    Open filename For Binary As ff
    buffer = ""
    charbuffer = ""
    For x = 1 To LOF(ff)
        Get #ff, , charbuffer
        If charbuffer <> vbCr And charbuffer <> vbLf Then
            buffer = buffer & charbuffer
        Else
            If buffer <> "" Then processBuffer buffer
            buffer = ""
        End If
    Next x
    Close ff


End Sub
Sub processBuffer(buffer As String)
    Dim varArray As Variant
    varArray = Split(buffer, vbTab)
    makeReplacements varArray(0), varArray(1)
End Sub
Sub makeReplacements(ByVal strToReplace As String, ByVal strReplacement As String)

    'MsgBox strToReplace & " will be replaced by " & strReplacement

    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = strToReplace
        .Replacement.Text = strReplacement
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
End Sub

Solution 2

Selection.Find.ClearFormatting
With Selection.Find
.Text = "English Word"
.Replacement.Text = "French Word"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

Create one of these for each word, but this won't read your table

Share:
5,191

Related videos on Youtube

Paulie
Author by

Paulie

Updated on September 18, 2022

Comments

  • Paulie
    Paulie over 1 year

    So I have a series of strings that I'd like to search and replace in Word documents.

    My strings would be displayed as a two-column table. I'd probably create the table with Word, then would convert it manually to TXT, or CSV or whatever would work... I don't need to automate that part. The table would look roughly like this:

    English word     French word
    dog     chien
    cat     chat
    London     Londres
    

    You get the idea...

    I need a macro that searches the terms contained in my first column, and replaces them with the content of the second column.

    Can you help? Thank you!

  • henry700
    henry700 about 12 years
    Note, you don't want to include a table header (i.e. english word French word) in your text document.