Discussion:
Find-replace from a table (array)
(too old to reply)
JWAzevedo
2009-03-29 01:50:01 UTC
Permalink
Raw Message
Hi,

I have a base document with a bunch of text. In a second document I've set
up a table of two columns. In the first column is the word/phrase I'd like
to find in the base document, and in the second column is the word/phrase I
like to replace that 'find' text with. So, basically, there's an array, and
I want to create a macro to take the whole array and do a series of
one-to-one find-replaces, without asking questions and without changing any
of the formatting in the base document. Can it be done?

Thanks!
Best,
Jerry
Doug Robbins - Word MVP on news.microsoft.com
2009-03-29 05:30:30 UTC
Permalink
Raw Message
Assuming that the items to be found and their replacements start in the
second row on the table, if a macro containing the following code is run
when the document in which the replacements are to be made is the active
document, it will display a dialog in which you can select the document that
contains the table of the items to be found and their replacements and when
you select that table, it will proceed to replace each instance of the text
to be found with its replacement for each row of the table

Dim Source As Document
Dim Target As Document
Dim fd As FileDialog
Dim i As Long
Dim frange As Range, rrange As Range
Set Target = ActiveDocument
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Select Document that contains the table of find/replace
items."
.InitialFileName = ""
If .Show = -1 Then
Set Source = Documents.Open(.SelectedItems(1))
Else
MsgBox "You must select the document that contains the table of
find/replace items."
End If
End With
Set fd = Nothing
With Source.Tables(1)
For i = 2 To Source.Tables(1).Rows.Count
Set frange = .Cell(i, 1).Range
frange.End = frange.End - 1
Set rrange = .Cell(i, 2).Range
rrange.End = rrange.End - 1
Target.Activate
With Selection
.HomeKey wdStory
.Find.Execute FindText:=frange.Text, MatchWildcards:=False, _
Format:=ClearFormatting, ReplaceWith:=rrange.Text,
Replace:=wdReplaceAll
End With
Next i
End With
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Post by JWAzevedo
Hi,
I have a base document with a bunch of text. In a second document I've set
up a table of two columns. In the first column is the word/phrase I'd like
to find in the base document, and in the second column is the word/phrase I
like to replace that 'find' text with. So, basically, there's an array, and
I want to create a macro to take the whole array and do a series of
one-to-one find-replaces, without asking questions and without changing any
of the formatting in the base document. Can it be done?
Thanks!
Best,
Jerry
JWAzevedo
2009-03-29 15:46:01 UTC
Permalink
Raw Message
Thanks a bunch, Doug. The macro works great. After some testing, I tweaked
the code so that it finds just the whole word and matches the case, and now
it does exactly what I want. I appreciate your help.

Best,
Jerry

Loading...