Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

share|improve this question
add comment

2 Answers

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|improve this answer
add comment

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
share|improve this answer
    
Note, you don't want to include a table header (i.e. english word French word) in your text document. –  horatio Feb 1 '12 at 17:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.