Written by Allen Wyatt (last updated October 26, 2023)
This tip applies to Word 97, 2000, 2002, and 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:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1462) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Mass Search and Replace.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
If you need to work with a copy of a document rather than the original document, you can use Word's Open dialog box to ...
Discover MoreOver the years Microsoft has made changes in Word. One change is to the import and export filters provided with the ...
Discover MoreIf you try to add spaces to the beginning of a document's file name, Word normally strips them away. This tip examines ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2017-05-09 16:31:03
Manny
You can also use a tool from www.officefindreplace.com to automate this easily.
2016-02-07 08:03:20
Hello
I am just beginning to learn about macros and purchased the Word Tip online which has the instruction for replacing texts in multiple documents on page 150/151. However, I noticed that it did not make any provision for also searching for .doc files in sub folders in the Macro example for 2007 and above docs. The Macro example provided for docs before Word 2007 has the File application search and sub-folder functionality but the macros for 2007 and above does not search in sub-folders but I want my search to also include sub-folders.
This was the example provided for searches in the Word Tip electronic manual for Word 2007 and above docs.
Could someone provide the relevant tweaking that would include sub-folder search please? I need this urgently.
Public Sub MassReplace2()
Dim Directory As String
Dim FType As String
Dim FName As String
Directory = "d:temp"
FType = "*.doc"
ChDir Directory
FName = Dir(FType)
' for each file you find, run this loop
Do While FName <> ""
' open the file
Documents.Open FileName:=FName
' search and replace the address
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.Text = "OldAddress"
.MatchCase = True
.Replacement.Text = "NewAddress"
End With
selection.Find.Execute Replace:=wdReplaceAll
' replace e-mail address
With selection.Find
.Text = "Oldemail"
.Replacement.Text = "Newemail"
End With
selection.Find.Execute Replace:=wdReplaceAll
' save and close the current document
ActiveDocument.Close wdSaveChanges
' look for next matching file
FName = Dir
Loop
End Sub
As noted, the above does not include any provision for sub-folder searches. I need a sample that will also search sub-folders because the files to be changed are not just in a single folder but in multiple sub-folders as well.
Thanks a lot for your anticipated help
Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments