Using macros to find and replace

J

Jan Johansson

I am doing a lot of translation, and sometimes it is helpful to conduct
a global search and replace words. I have now been told that it is
possible to write a macro which can find and replace several words in
one go. (today I am doing it word by word) Do you have any idea on how
to do this?

Jan J.
 
C

cyberdude

I am doing a lot of translation, and sometimes it is helpful to conduct
a global search and replace words. I have now been told that it is
possible to write a macro which can find and replace several words in
one go. (today I am doing it word by word) Do you have any idea on how
to do this?

Jan J.

Can Word's Find_and_replace also help? (press Ctrl-H to activate the
function)

Mike
 
G

Graham Mayor

cyberdude said:
Can Word's Find_and_replace also help? (press Ctrl-H to activate the
function)

Mike
Find and replace will not work from a list of Words, though the replace
function will certainly work from lists if called from a macro. Greg's
solution is probably simplest as he has done all the work, but the following
macro uses a two column table saved in the document - sFname = "D:\My
Documents\Test\changes.doc" and replaces the words in the current document
from the first column with those in the second


Sub ReplaceFromTableList()

Dim ChangeDoc As Document, RefDoc As Document
Dim cTable As Table
Dim oldPart As Range, newPart As Range
Dim i As Long
Dim sFname As String

sFname = "D:\My Documents\Test\changes.doc"
Set RefDoc = ActiveDocument
Set ChangeDoc = Documents.Open(sFname)
Set cTable = ChangeDoc.Tables(1)
RefDoc.Activate
For i = 1 To cTable.Rows.Count
Set oldPart = cTable.Cell(i, 1).Range
oldPart.End = oldPart.End - 1
Set newPart = cTable.Cell(i, 2).Range
newPart.End = newPart.End - 1
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Execute findText:=oldPart, _
ReplaceWith:=newPart, _
Replace:=wdReplaceAll, _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindContinue
End With
End With
Next i
ChangeDoc.Close wdDoNotSaveChanges
End Sub

or you could use arrays, which is quicker, though not as convenient for long
lists eg

Sub ReplaceList()
Dim vFindText As Variant
Dim vReplText As Variant
Dim i As Long

vFindText = Array(Chr(33), "Lorem", "ipsum", "dolor")
vReplText = Array(Chr(46), "WORD1", "WORD2", "WORD3")
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = True

For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = vReplText(i)
.Execute Replace:=wdReplaceAll
Next i
End With
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top