Massive Search & Replace in Word

A

Ann Scharpf

My company recently completely overhauled a fairly large
software system. As part of the upgrade, all the fields
were renamed with a new naming convention. Now we must
update the documentation with the new names.

There are approximately 500 files and about 4,850
replacement terms for the old field names. I'd like to
create a table with old and new terms and have a macro
parse the table and do the replacements.

I originally posted this question on the Word page. Greg
Maxey helped me out by giving me the following VBA macro.
This macro works on one file at a time. I created a small
table and did a test and it works fine.

My question is ... how many search & replace rows can I
safely have in a table? I.e. how many replace functions
can I have Word do without blowing up? I'm sure I can't
include all the 4,850 replaces in a single macro but I'd
like to divide it up into the fewest number of macros
possible, since we have to run the macros on 500 files.
(I don't know VBA and don't know how to marry up this
macro with some macros that have been posted for searching
& replacing across all the files in a folder.)

Our word processing team is working on computers that
still have Word 97 and Word 2000 on them. Operating
system is Windows 2000. Their machines have 130 MB RAM.

Here is the macro. Thanks for any advice you can give me.

Ann Scharpf
_______________________________

Sub TechRef_S_R()
'
' TechRef_S_R Macro
' Macro created 06/16/04 by ANN.SCHARPF
'
'
'
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:="C:\TechRef\SRlist.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
 
H

Helmut Weber

Hi Ann,
to the best of my experience rather than of my knowledge,
it seems that this depends on how many replacements are
successful within a single document. Unsuccessful replacements
don't do any harm. With Word 97, however, there seems to
be an unkown limit of editing operations, which may be
encreased by clearing the undo-buffer "ActiveDocument.UndoClear"
every now and then, and setting "options.AllowFastSave = False"
and saving, of course.
I'm doing things like that using VB, on thousands of docs.
That is, calling word, processing docs, closing word.
One "Word" per folder, to be precise.
You may use Excel-VBA, if you don't have VB.
(I don't know VBA hmm... really hard.
and don't know how to marry up this macro with some macros
that have been posted for searching
& replacing across all the files in a folder.)
That is the easiest thing. Google for "filesearch".
And for speed beware of "selection". Get used to "range".
It is lightening fast.
And, talking about "fields", the macro you've posted searches
for text, not for fields or fields' results.
Hard work, lot of work.
 
P

Peter Hewett

Hi Ann

Word can be somewhat unstable when processing lots of files. I either split the files up
into small batches or write a VB Script shell that starts Word, has it process the file
then dispose of Word, and do this for each file you want processed.

You can use either the Word Application.FileSearch or the FileSystemObject object to built
a list of files to convert. If you want to use a VB Script shell you'll need to use the
FileSystemObject. Here's a link to the Application.FileSearch method:

http://www.word.mvps.org/faqs/macrosvba/BatchFR.htm


I'd also create an event log for this operation so that you *know* exactly what's been
changed. If you need to you could even log the number of replacements (or at least non
zero replacements) for each Search/Replace pair.

As Helmut said, clear the Undo buffer frequently and save the document frequently, it
avoids numerous nasty problems.

Since this is a one-off exercise the efficiency of the code is fairly irrelevant it's the
efficacy that counts!

Good luck + Cheers - Peter


Hi Ann,
to the best of my experience rather than of my knowledge,
it seems that this depends on how many replacements are
successful within a single document. Unsuccessful replacements
don't do any harm. With Word 97, however, there seems to
be an unkown limit of editing operations, which may be
encreased by clearing the undo-buffer "ActiveDocument.UndoClear"
every now and then, and setting "options.AllowFastSave = False"
and saving, of course.
I'm doing things like that using VB, on thousands of docs.
That is, calling word, processing docs, closing word.
One "Word" per folder, to be precise.
You may use Excel-VBA, if you don't have VB.
That is the easiest thing. Google for "filesearch".
And for speed beware of "selection". Get used to "range".
It is lightening fast.
And, talking about "fields", the macro you've posted searches
for text, not for fields or fields' results.
Hard work, lot of work.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98

HTH + Cheers - Peter
 
A

Ann Scharpf

Helmut:

Thanks for your response! I thought the key thing in
the "how many terms" question might be how many
replacements were actually performed. That seems to be
what chews up the memory.

I did not understand what you meant by:
And for speed beware of "selection". Get used to "range".
It is lightening fast.
Can you explain this?

As for the "ActiveDocument.UndoClear"
and "options.AllowFastSave = False" ... Is there a
particular place they would need to appear in the macro
code?

Also, when I say "fields" I only mean they are fields in
the program I am documenting. The strings are actually
just text in a document when I am doing the replacements.

Thanks again!

Ann
-----Original Message-----
Hi Ann,
to the best of my experience rather than of my knowledge,
it seems that this depends on how many replacements are
successful within a single document. Unsuccessful replacements
don't do any harm. With Word 97, however, there seems to
be an unkown limit of editing operations, which may be
encreased by clearing the undo-
buffer "ActiveDocument.UndoClear"
 
A

Ann Scharpf

Thanks, Peter! I will look at this link this morning!

I think I need to take a Visual Basic class.

Ann
-----Original Message-----
Hi Ann

Word can be somewhat unstable when processing lots of
files. I either split the files up
into small batches or write a VB Script shell that starts Word, has it process the file
then dispose of Word, and do this for each file you want processed.

You can use either the Word Application.FileSearch or the
FileSystemObject object to built
a list of files to convert. If you want to use a VB
Script shell you'll need to use the
FileSystemObject. Here's a link to the Application.FileSearch method:

http://www.word.mvps.org/faqs/macrosvba/BatchFR.htm


I'd also create an event log for this operation so that you *know* exactly what's been
changed. If you need to you could even log the number of replacements (or at least non
zero replacements) for each Search/Replace pair.

As Helmut said, clear the Undo buffer frequently and save the document frequently, it
avoids numerous nasty problems.

Since this is a one-off exercise the efficiency of the
code is fairly irrelevant it's the
 
H

Helmut Weber

Hi Ann,
a lot depends on whether the documents are rather
small or rather large. 4,850 replacement terms are
not a problem. The problem is the number of document
changes. There seems to be an unkown limit.
And for speed beware of "selection". Get used to "range".
I am sorry about that. It was to the best of my knowledge
until today. Nobody contradicted. But when I tested it on
a 400 pages document, I couldn't find any evidence, that
either "selection" or "range" were faster.
As for the "ActiveDocument.UndoClear"
and "options.AllowFastSave = False"
"AllowFastSave = False" should be placed at the beginning
of the macro. You might like to reset it to true at the end.
It slows saving down and encreases stability. Which way to go?
With ActiveDocument.UndoClear it is quite the same. To clear
the undo-buffer needs time and encreases stability. To clear
the undo-buffer if it is empty is useless. To avoid that,
you would have to record successfull replacements. Costs time again.
Whatever you do, there is always a drawback.
I got about 50 users, who produce small docs day in and day out,
and a VB-timer, that starts the master VB-program at 3 o'clock in
the morning, that selects the word-docs and starts Word.
Word writes "start" in a log file and the docs fullname, tries to
do the replacements, and, if it didn't crash, writes "end" in a log
file. If "end" is missing, I know, which file caused the trouble.
Usefull only if troublemakers are exceptional, otherwise ...
It is endless...
 
W

Word Heretic

G'day Helmut Weber <[email protected]>,

In spite of speed differences, I find the range code much smaller and
cleaner than selection code. For example, you don't have to save and
restore the find settings, let alone finickly mandating every option.

That being said, there is some stuff that only works with a selection,
and not with a range.


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Helmut Weber reckoned:
 

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