0
votes

I use this macro to replace some values in different word documents, but it doesn't replace the text in the footer of the first page. I guess it's because the first page footer is different then the others. I just started to work with VBA codes, so I don't know how to solve this problem. I would be really grateful, if someone could modify the macro for me, so that it can find and replace values in the first page's footer, too?

Sub DoReplace()

Const Find1 = "FIND TEXT"
Const Replace1 = "REPLACE TEXT"

Const Find2 = "FIND TEXT"
Const Replace2 = "REPLACE TEXT"

Dim FilePick As FileDialog
Dim FileSelected As FileDialogSelectedItems
Dim WordFile As Variant  ' FileName placeholder in selected files loop
Dim FileJob As String    ' Filename for processing

Dim WorkDoc As Object
Dim WholeDoc As Range
Dim FooterDoc As Range

On Error GoTo DoReplace_Error

    Set FilePick = Application.FileDialog(msoFileDialogFilePicker)

    With FilePick
        .Title = "Choose Report Template"
        .Filters.Clear
        .Filters.Add "Word Documents & Templates", "*.do*"
        .Filters.Add "Word 2003 Document", "*.doc"
        .Filters.Add "Word 2003 Template", "*.dot"
        .Filters.Add "Word 2007 Document", "*.docx"
        .Filters.Add "Word 2007 Template", "*.dotx"
        .Show
    End With

    Set FileSelected = FilePick.SelectedItems

    If FileSelected.Count <> 0 Then

        For Each WordFile In FileSelected

            FileJob = WordFile

            Set WorkDoc = Application.Documents.Open(FileJob, , , , , , , , , , , False)

            Set WholeDoc = WorkDoc.Content
            Set FooterDoc = WorkDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range

            With FooterDoc
                .Find.Execute Find1, True, True, , , , True, , , Replace1, wdReplaceAll
                .Find.Execute Find2, True, True, , , , True, , , Replace2, wdReplaceAll
            End With

            With WholeDoc.Find
                .Execute Find1, True, True, , , , True, , , Replace1, wdReplaceAll
                .Execute Find2, True, True, , , , True, , , Replace2, wdReplaceAll

            End With

            WorkDoc.Save
            WorkDoc.Close

        Next

    End If

    MsgBox "Completed"

DoReplace_Exit:

    Set WholeDoc = Nothing
    Set FilePick = Nothing

    Set WorkDoc = Nothing
    Set FooterDoc = Nothing

    Exit Sub

DoReplace_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure DoReplace of VBA Document ReplaceMulti"
    Resume DoReplace_Exit

End Sub
1

1 Answers

0
votes

Create another variable and assign the other Footer to it. Something like

Dim footerPage1 as Word.Range
Set footerPage1 = WorkDoc.Sections(1).Footers(wdHeaderFooterFirstPage).Range

HOWEVER there is a better way to go about searching in the entire document, without needing to know about and specify each and every "Story". Start by looking up the documentation on the property: StoryRanges

This contains sample code that will give you a start. Then do a search in the Internet on the following terms to read up on how to use it in detail:

Word find StoryRanges