How Does One Add a Page Number to a Header?

S

StevenM

I would like to create a header so that: “Add Title Here†appears on the left
side of the page, and “Page {PAGE}†appears on the right side of the page. So
far I got:

Sub AddHeader()
Dim oRange As Range

Set oRange = ActiveDocument.Range(Start:=ActiveDocument.Range.Start,
End:=ActiveDocument.Range.End)
With oRange.Sections(1).Headers(wdHeaderFooterPrimary).Range
..Delete
..Paragraphs.Alignment = wdAlignParagraphLeft
..InsertAfter "Add Title Here" & vbTab & vbTab & "Page "
End With
End Sub

I would like a page number to follow: "Page ", I wish I could just add: &
vbPageNumber, but I suspect that it will be more complex than that.

Any help will be greatly appreciated.

Steven Craig Miller

P.S. I apologize for having wrongly placed a duplicate message in Word Tables.
 
D

Doug Robbins - Word MVP

I would suggest that you create a template that already has the header set
up in it and use a userform to ask the user for the Title of the document
and have code in the form save the Title to a document variable varTitle and
have a { DOCVARIABLE varTitle } field in the header to display the title.

See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

StevenM

I think I figured it out. The following seems to work:

Sub AddHeader()
Dim oRange As Range
Dim headerRange As Range

Set oRange = ActiveDocument.Range(Start:=ActiveDocument.Range.Start,
End:=ActiveDocument.Range.End)
Set headerRange = oRange.Sections(1).Headers(wdHeaderFooterPrimary).Range

With headerRange
..Delete
..InsertAfter "Title:" & vbTab & vbTab & "Page "
..MoveEnd Unit:=wdCharacter, Count:=1
..Collapse wdCollapseEnd
oRange.Fields.Add Range:=headerRange, Type:=wdFieldPage
End With
End Sub

Steven Craig Miller
 
F

fumei via OfficeKB.com

You would be MUCH better off doing as it was suggested. Have this as a
template. Especially one that uses Styles properly.
 
S

StevenM

: << You would be MUCH better off doing as it
was suggested. Have this as a template. Especially one that uses Styles
property. >>

Is it possible to have two (or more) headers using a styles property? So
that if one option is chosen (via a user form) then header "A" can be used,
and if another option is chosen then header "B" is used?

-Steven Craig Miller
 
F

fumei via OfficeKB.com

Yes. But why would they have to have different styles? Different styles
does not mean different content. You can have different content, but the
same style.

You can do what ever you want (pretty much).

If you have to have different headers - depending on a user choice selected
from a userform - then you can easily put that choice into the header using
AutoText.

In the example below, I made two Autotext entries in the template. HeaderA
and HeaderB. I made them very different, but that DOES NOT MATTER.

I used an InputBox...but that also does not matter. You can just as easily
do it from a userform on Document_New (when you clone a new document from the
template. What matters is you now have a choice.

Dim whatever
whatever = InputBox("A or B")
Select Case whatever
Case "A"
ActiveDocument.AttachedTemplate.AutoTextEntries("HeaderA") _
.Insert _
Where:=ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).
Range, _
RichText:=True
Case "B"
ActiveDocument.AttachedTemplate.AutoTextEntries("HeaderB") _
.Insert _
Where:=ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).
Range, _
RichText:=True
Case Else
MsgBox "Huh"
End Select

Enter "A".....HeaderA gets dumped into the header.

Enter "B".....HeaderB gets dumped into the header.

As it happens, I made HeaderA have an image (a logo), some text, and a PAGE
field, using a style I named whoCares_1

I made HeaderB have text, tabs, and a PAGE field, using a style I named
WhoREALLYcares.

IT DOES NOT MATTER. The code inserts the AutoText as it is, whatever it is.
 

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