• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

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.
Example:
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

0
Andreas Hermle
Asked:
Andreas Hermle
1 Solution
 
GrahamSkanRetiredCommented:
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
                    Do
                        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
                Loop
            End With
        Next hdr
    Next sec
End Sub

Open in new window

0
 
Andreas HermleTeam leaderAuthor Commented:
Hey Graham, thank you so much. As always a perfect solution. Regardsd, Andreas
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now