
Tips.Net > WordTips Home > Files > Mass Search and Replace
Summary: Word does not provide a way to do a find and replace through a group of documents. The macro and third-party solutions presented in this tip will help you find a way to find and replace exactly what you need. (This tip works with Microsoft Word 97, Word 2000, Word 2002, and Word 2003.)
Over time it is very possible to collect a huge number of documents. At some point you may want to make the same change to each of the documents in the collection. For instance, you may need to change the company name within each document. Obviously you can open each document, make the change, and then save the document, but that process can quickly become tiring if you have hundreds or thousands of documents to process.
What to do? Consistent with the point that has been made in other issues of WordTips, anytime you have something that is mundane and tiresome to accomplish, you can often use a macro to handle the work for you. For instance, you could write a macro that would step through all the documents in a directory, load each in turn, search for and change the necessary text, and resave the document. This process is no different than the process you would follow manually, except that it is done under the control of the macro. This makes it much easier and faster.
The following is an example of a macro that could do the trick:
Public Sub MassReplace()
With Application.FileSearch
.LookIn = "C:\" ' where to search
.SearchSubFolders = True ' search the subfolders
.FileName = "*.doc" ' file pattern to match
' if more than one match, execute the following code
If .Execute() > 0 Then
' to display how many files this macro will access,
' uncomment the next line of code
' MsgBox "Found " & .FoundFiles.Count & " file(s)."
' for each file you find, run this loop
For i = 1 To .FoundFiles.Count
' open the file based on its index position
Documents.Open FileName:=.FoundFiles(i)
' search and replace the company name
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.Text = "OldCompanyName"
.MatchCase = True
.Replacement.Text = "NewCompanyName"
End With
selection.Find.Execute Replace:=wdReplaceAll
' replace street address
With selection.Find
.Text = "OldStreetAddress"
.Replacement.Text = "NewStreetAddress"
End With
selection.Find.Execute Replace:=wdReplaceAll
' replace the City, State, and Zip code
With selection.Find
.Text = "OldCityStateAndZip"
.Replacement.Text = "NewCityStateAndZip"
End With
selection.Find.Execute Replace:=wdReplaceAll
' save and close the current document
ActiveDocument.Close wdSaveChanges
Next i
Else
' if the system cannot find any files
' with the .doc extension
MsgBox "No files found."
End If
End With
End Sub
This macro is quite powerful, and it allows you to not just change a company name, but also your company's address. All you need to do is make changes to specify which directory and drive to use in your search, as well as what the old and new company information is.
If dealing with macros is a little beyond what you want to tackle, there are also a number of different commercial products available that will work with Word documents. Various subscribers have suggested the following programs:
Tip #1462 applies to Microsoft Word versions: 97 2000 2002 2003
More Power! Expand your skills and make Word really sing! It's all possible with macros. The best resource anywhere for macros is WordTips: The Macros. Check it out today!
Uncover how you can master the full potential of printing your documents. Everything you wanted to know about printing and printers, using Word. (more information...)
Ask a Word Question
Make a Comment
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Pet Tips
Word2007 Tips
WordTips