How to Use BatchReplaceAll to Replace Font in Footer RRS feed

  • Question

  • don't know how to write visual basic but understand most code when reading.  i need to replace eras book and eras demi to eras lt book and eras lt demi in over 500 Word documents.

    have combined pieces of code (see below) found online to find / replace a font  in multiple Word documents, but can't get it to work for footers.  any help to make this code work for  footers is appreciated.

    Sub BatchReplaceAll()


    Dim FirstLoop As Boolean

    Dim myFile As String

    Dim PathToUse As String

    Dim myDoc As Document

    Dim Response As Long


    ' Use this folder selection for Word versions 2002-7

    Dim fDialog As FileDialog

    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)


    ' Get the folder containing the files

    With fDialog

         .Title = "Select Folder containing the documents to be modified and click OK"

         .AllowMultiSelect = False

         .InitialView = msoFileDialogViewList

         If .Show <> -1 Then

              MsgBox "Cancelled By User", , "Batch Replace Anywhere"

              Exit Sub

         End If

         PathToUse = fDialog.SelectedItems.Item(1)

         If Right(PathToUse, 1) <> "\" Then PathToUse = PathToUse + "\"

    End With


    'Close any documents that may be open

    If Documents.Count > 0 Then

         Documents.Close Savechanges:=wdPromptToSaveChanges

    End If

    FirstLoop = True



    'End of Folder selection



    '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 & "*.docx")


    While myFile <> ""


        'Open document

        Set myDoc = Documents.Open(PathToUse & myFile)


        If FirstLoop Then


            'Display dialog on first loop only




            FirstLoop = False


            Response = MsgBox("Do you want to process " & _

            "the rest of the files in this folder", vbYesNo)

            If Response = vbNo Then Exit Sub




            'On subsequent loops (files), a ReplaceAll is

            'executed with the original settings and without

            'displaying the dialog box again


            With Dialogs(wdDialogEditReplace)

                .ReplaceAll = 1


            End With


        End If


        'Close the modified document after saving changes


        myDoc.Close Savechanges:=wdSaveChanges


        'Next file in folder


        myFile = Dir$()




    End Sub

    • Moved by Mark Liu-lxfModerator Tuesday, November 1, 2011 6:44 AM vba question (From:Visual Basic General)
    Monday, October 31, 2011 3:26 AM

All replies