• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

4. Editing Power Tools > 31. Find/Replace in Multiple Files

Find/Replace in Multiple Files

Make the same substitution on several files at once using the code in this hack.

Find and Replace is a real time-saver, but when you have to perform the same substitution on multiple files, it can seem like more of a hindrance than a help.

If you regularly perform the same types of substitutions on multiple documents, developing a set of macros to do the work for you can represent a real improvement in efficiency.

The Code

For example, say your law firm, Dewey & Cheatham, just added a partner, and now you’re Dewey, Cheatham & Howe. The following macro searches all the Word documents in the folder C:\My Documents and replaces the old name with the new name wherever it occurs:

Sub FindReplaceAllDocsInFolder( )
Dim i As Integer
Dim doc As Document
Dim rng As Range

With Application.FileSearch
    .NewSearch
    .LookIn = "C:\My Documents"
    .SearchSubFolders = False
    .FileType = msoFileTypeWordDocuments
    If Not .Execute( ) = 0 Then
        For i = 1 To .FoundFiles.Count
             Set doc = Documents.Open(.FoundFiles(i))
             Set rng = doc.Range
              With rng.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = "Dewey & Cheatem"
                .Replacement.Text = "Dewey, Cheatham & Howe"
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
         ....

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint