[Last Call] Learn how to a build a cloud-first strategyRegister Now


Delete certain text in header just once using VBA

Posted on 2008-11-09
Medium Priority
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

GrahamSkan earned 2000 total points
ID: 22918331
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

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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
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…
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 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