Deleting the word "Page" in Header

A

andreas

Dear Experts:

The headers in my document have this make-up

{STYLEREF FIELD 1 / n} Text Page
{PAGENUMBER}

For certain reasons I would like to delete the word "page" not using
the search and replace functionality but by setting a range for the
header. The range should then move till the end of the paragraph and
then go backwards past the field {PAGENUMBER} till the word "page" is
reached, select that word and delete it. There is a space between the
word page and the Page Field

May sound strange but I just would like to know how to write this in
VBA. Is it feasible?

Thank you very much in advance.

Regards, Andreas
 
H

Helmut Weber

Hi Andreas,

maybe like that:

Sub Testkk()
Dim rTmp As Range
Set rTmp =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
rTmp.Collapse Direction:=wdCollapseEnd
rTmp.start = rTmp.End - 5
While rTmp.Text <> "Page "
' move the range
rTmp.start = rTmp.start - 1
rTmp.End = rTmp.End - 1
' rTmp.Select ' for testing
Wend
rTmp.Delete
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

Well,

that, of course, is assuming
that "Page " is found in the header.

For avoiding an endless loop,
if "Page " is not found:

Sub Testkk()
Dim rTmp As Range
Set rTmp =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
rTmp.Collapse Direction:=wdCollapseEnd
rTmp.start = rTmp.End - 5
While rTmp.Text <> "Page " And _
rTmp.start <>
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.start
rTmp.start = rTmp.start - 1
rTmp.End = rTmp.End - 1
' rTmp.Select
Wend
If rTmp.Text = "Page " Then
rTmp.Delete
End If
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
A

andreas

Well,

that, of course, is assuming
that "Page " is found in the header.

For avoiding an endless loop,
if "Page " is not found:

Sub Testkk()
Dim rTmp As Range
Set rTmp =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
rTmp.Collapse Direction:=wdCollapseEnd
rTmp.start = rTmp.End - 5
While rTmp.Text <> "Page " And _
rTmp.start <>
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.start
   rTmp.start = rTmp.start - 1
   rTmp.End = rTmp.End - 1
'    rTmp.Select
Wend
If rTmp.Text = "Page " Then
   rTmp.Delete
End If
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

Hey Helmut,

as always with your coding, it works just fine. Thank you very much.
Regards, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top