Delete certain text in header just once using VBA

Posted on 2008-11-09
Last Modified: 2012-05-05
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

Question by:AndreasHermle
    LVL 76

    Accepted Solution

    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


    Author Closing Comment

    Hey Graham, thank you so much. As always a perfect solution. Regardsd, Andreas

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
    Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
    This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now