MASSIVE search & replace question

A

Ann Scharpf

Is it possible to pass parameters from a table to a search
& replace macro? We have recently completely overhauled a
fairly large software system. As part of the upgrade to a
new operating system, all the fields were renamed with a
new naming convention. Now the documentation must be
modified to match the new names.

There are approximately 500 files that need to have the
replacing done on them. There are literally THOUSANDS of
search & replace terms for the new field names.

I have looked at the archives and see that there are VBA
macros for replacing strings in all the files in a
folder. But I have not seen any way to create a list of
search & replace items for a macro to parse through. Is
this possible?

Thanks so much for any guidance you can give me.

Ann Scharpf
 
G

Greg Maxey

Ann,

The following macro will parse (that is the word you used :) ) a table for
the find and replace text. I don't know how, but you might be able to apply
it to the macro that use mentioned for performing a find and replace on a
batch of files:
Sub MultiFindAndReplace()
'
'
Dim WordList As Document
Dim Source As Document
Dim i As Integer
Dim Find As Range
Dim Replace As Range
Set Source = ActiveDocument
' Change the path and filename in the following to suit where you have your
list of words
Set WordList = Documents.Open(FileName:="D:\My Documents\Word Documents\Find
and Replace List.doc")
Source.Activate
For i = 2 To WordList.Tables(1).Rows.Count
Set Find = WordList.Tables(1).Cell(i, 1).Range
Find.End = Find.End - 1
Set Replace = WordList.Tables(1).Cell(i, 2).Range
Replace.End = Replace.End - 1
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = Find
.Replacement.Text = Replace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next i

End Sub
 
J

Jezebel

Create a source file containing the search and replace pairs. Given that you
have so many of them, an Excel spreadsheet might be convenient to work with
and validate; a flat text file would be also work well, as would a Word
document containing a table.

In your macro, read the source file into a two-dimensional array (if your
source is Excel, you can do that in a single statement); then iterate your
array calling the Search and Replace function once for each row.
 
A

Ann Scharpf

Greg:

Wow! Thanks a lot. I am about to do a baby test to make
sure I understand how to use this. One question does come
to mind...

I have THOUSANDS of terms to replace. Is there a maximum
number of rows you'd recommend I include in a table? I
could split the information into multiple find & replace
files, (with a macro for each.

Ann
 
A

Ann Scharpf

Greg:

I modified the macro slightly. (The path name to s&r doc,
as you mention and the name of the macro. I think those
are the only changes I made.)

I then created a two column document with search & replace
terms. (Search string in left column; replace string in
right column.)

When I run the macro, Word opens the S&R document but none
of my terms gets replaced. Is there a step I am missing?

Thanks.

Ann
 
A

Ann Scharpf

Came back after lunch and macro was working fine. Maybe I
was the one not functioning properly!

Still do have the question about the maximum # of
replacement functions you think it is ok to leave in a
table for a single macro.

Thanks so much for your help! This saved our word
processing team countless hours of manual work!

Ann Scharpf
 
G

Greg Maxey

Ann,

I am please that you were able to get the macro working. I have never used
it in a practical application so I can't advise on how much you can expect
from it wrt to number of find and replace sequences.

You might want to post your adapted macro in the VBA General group and
perhaps one of the experts will advise.

Cheers
 

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