none
Word 2007 macro replace header & footer

    Question

  • Hello,

    I run into very inconsistent behaviour in Word 2007. Hopefully it is due to my ignorance. Hopefully someone can enlighten me.

    I create a new blank word 2007 document

    In the document I put the word "repl"
    I create a header and footer with the word "repl" in it.

    That's all. Document saved.

    I record a macro using the macro recorder.

    Sub repl()
    '
    ' repl Macro
    '
    '
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "repl"
            .Replacement.Text = "Replaced"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub


    All three occurences of the word "repl" are indeed replaced by the text "Replaced"

    Then I reopen the document and run the macro again.
    This can be done from the macro menu or from the VB editor, but ONLY the word "repl" in the text is replaced. Header and footer are left untouched.

    Please enlighten me.

    Thanks
    Friday, June 06, 2008 9:45 AM

All replies

  • I found the solution

     

    This will replace the text in the footer only though. You need to execute the Selection.Find.Execute-function for the body, the footer and the header

     

    Olly

     

    Code Snippet

    Sub repl()
    '
    ' repl Macro
    '
    '

    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter

     

       Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "repl"
            .Replacement.Text = "Replaced"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll


     

    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

     

    End Sub

     

     

     

    • Proposed as answer by Olly_Cologne Thursday, May 03, 2012 10:19 AM
    Wednesday, November 19, 2008 10:43 PM