Delete certain text in header just once using VBA

Dear Experts:
I would like to remove the word 'Page' that immediately precedes the page field in headers (primary/even/uneven headers) of a document. The headers may have another occurrence of the word 'page", but this occurence must not be deleted.
text text 'PAGE' text  text     'PAGE' { Page \* Arabic \* MERGEFORMAT}  

Is it possible - as I mentioned above - to remove just the word "PAGE" immediately preceding the page field and leave the other occurence of PAGE untouched?

Thank you very much in advance for your help. Regards, Andreas

Andreas HermleTeam leaderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi Andreas,

I think that this will do it.
Option Explicit
Sub ReplacePageWord()
    Dim sec As Section
    Dim hdr As HeaderFooter
    Dim rng As Range
    For Each sec In ActiveDocument.Sections
        For Each hdr In sec.Headers
        Set rng = hdr.Range
            With rng.Find
                .MatchWildcards = True
                .Text = "<Page>"
                Do While .Execute
                        rng.MoveEnd wdCharacter, 1
                    Loop Until Right$(rng.Text, 1) <> " "
                    If rng.Fields.Count = 1 Then
                        rng.MoveEnd wdCharacter, -1
                        rng.Text = ""
                        Exit Do
                    End If
                    rng.Collapse wdCollapseEnd
                    rng.End = hdr.Range.End
            End With
        Next hdr
    Next sec
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andreas HermleTeam leaderAuthor Commented:
Hey Graham, thank you so much. As always a perfect solution. Regardsd, Andreas
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.