How to Find & ReplaceAll on a batch of documents in the same folder

Article contributed by Ibby

The following code, if stored in a Global template, will perform a Find & ReplaceAll in all of the documents in a specified folder. The FindReplace dialog is displayed for the first document only. The user sets the parameters in the dialog and presses Replace All and then Close. The user is then asked whether to process all of the files in the specified directory – if Yes, the rest of the files are processed with the settings as entered in the original FindReplace dialog.

Option Explicit

Public Sub BatchReplaceAll()

Dim FirstLoop As Boolean
Dim
myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long

PathToUse = "C:\Test\"

'Error handler to handle error generated whenever
'the FindReplace dialog is closed

On Error Resume Next

'Close all open documents before beginning

Documents.Close SaveChanges:=wdPromptToSaveChanges

'Boolean expression to test whether first loop
'This is used so that the FindReplace dialog will
'only be displayed for the first document

FirstLoop = True

'Set the directory and type of file to batch process

myFile = Dir$(PathToUse & "*.doc")

While myFile <> ""

    'Open document
    Set myDoc = Documents.Open(PathToUse & myFile)

    If FirstLoop Then

        'Display dialog on first loop only

        Dialogs(wdDialogEditReplace).Show

        FirstLoop = False

        Response = MsgBox("Do you want to process " & _
        "the rest of the files in this folder", vbYesNo)
        If Response = vbNo Then Exit Sub

    Else

        'On subsequent loops (files), a ReplaceAll is
        'executed with the original settings and without
        'displaying the dialog box again

        With Dialogs(wdDialogEditReplace)
            .ReplaceAll = 1
            .Execute
        End With

    End If

    'Close the modified document after saving changes

    myDoc.Close SaveChanges:=wdSaveChanges

    'Next file in folder

    myFile = Dir$()

Wend

End Sub

If there is a posibility that some files might be password protected, see also: Skipping Password-Protected Documents in a Batch Process.

If you want to perform the Find & ReplaceAll in all subfolders as well, use the FileSearch object instead of Dir.  Dir is significantly faster than FileSearch when searching a single directory – and it's also simpler to code; but when searching all subdirectories as well, it's simplest to use  FileSearch, e.g.:

Option Explicit

Public Sub BatchReplaceAll()

Dim FirstLoop As Boolean
Dim
myFile As String
Dim
PathToUse As String
Dim myDoc As Document
Dim
Response As Long
Dim
i As Long

PathToUse = "C:\Test\"

'Error handler to handle error generated whenever
'the FindReplace dialog is closed

On Error Resume Next

'Close all open documents before beginning

Documents.Close SaveChanges:=wdPromptToSaveChanges

'Boolean expression to test whether first loop
'This is used so that the FindReplace dialog will
'only be displayed for the first document

FirstLoop = True

'Set the directory and type of file to batch process

With Application.FileSearch
   .NewSearch
   .LookIn = PathToUse
   .SearchSubFolders = True
   .FileName = "*.doc"
   .MatchTextExactly = True
   .FileType = msoFileTypeAllFiles

    If .Execute() Then

        For i = 1 To .FoundFiles.Count

            'Open document
            Set myDoc = Documents.Open(.FoundFiles(i))

            If FirstLoop Then

                'display dialog on first loop only

                Dialogs(wdDialogEditReplace).Show

                FirstLoop = False

                Response = MsgBox("Do you want to process " & _
                "the rest of the files in this folder", vbYesNo)
                If Response = vbNo Then Exit Sub

            Else

                'On subsequent loops (files), a ReplaceAll is
                'executed with the original settings and without
                'displaying the dialog box again


                With Dialogs(wdDialogEditReplace)
                    .ReplaceAll = 1
                    .Execute
                End With

            End If

            'Close the modified document after saving changes
            myDoc.Close SaveChanges:=wdSaveChanges

        Next i

    End If

End With

End Sub


Click to view Terms of Use page

Click to view Disclaimer page