Inserting a Page Number in a Table in a Header

M

Michael Gordon

I'm trying to insert a page number field inside a table inside a header.
Basically, I want the text in Cell 1,1 to print, then Cell 1,2 should have
"Page X" in it. Here's what I have so far:

With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
'Wipe out header
..Select
Selection.Delete
'Add table
..Tables.Add
Range:=ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range,
NumRows:=1, NumColumns:=2
..Tables(1).Cell(1, 1).Select
Selection.TypeText Text:="Text goes here"
..Tables(1).Cell(1, 2).Select
'Selection.TypeText Text:=
Dim rng As Range
Set rng = .Tables(1).Cell(1, 2).Range
rng.ParagraphFormat.Alignment = wdAlignParagraphRight
rng.Text = "Page "
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, wdFieldPage

Unfortunately, at that last line, I get Run-Time Error 4605: "This command
is not available."

I've narrowed it down to the fact that it's in a table. How can I fix this?
I'm working on Word 2007 12.0.6311.5000.

Thanks!
 
S

StevenM

To: Michael,

You're last two lines are:
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, wdFieldPag

What you need is:
rng.Collapse wdCollapseEnd
rng.Move wdCharacter, -1
ActiveDocument.Fields.Add Range:=rng, Type:=wdFieldPage

As I understand it (perhaps someone will correct me), you can't add a field
to a Range, but have to add it to a document at a Range. So for example:

Dim oDoc as Document
Dim oRange as Range

Then you could add a field such as:

Dim oDoc.Field.Add Range:=oRange, etc.

Also, there appears to be a table character outside every row. So when you
went:
rng.Collapse wdCollapseEnd
It moved the range just outside the table. So I added:
rng.Move wdCharacter, -1
Which put the range back inside the table, and it worked.

Steven Craig Miller
 
M

Michael Gordon

That worked! Thanks!

StevenM said:
To: Michael,

You're last two lines are:
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, wdFieldPag

What you need is:
rng.Collapse wdCollapseEnd
rng.Move wdCharacter, -1
ActiveDocument.Fields.Add Range:=rng, Type:=wdFieldPage

As I understand it (perhaps someone will correct me), you can't add a field
to a Range, but have to add it to a document at a Range. So for example:

Dim oDoc as Document
Dim oRange as Range

Then you could add a field such as:

Dim oDoc.Field.Add Range:=oRange, etc.

Also, there appears to be a table character outside every row. So when you
went:
rng.Collapse wdCollapseEnd
It moved the range just outside the table. So I added:
rng.Move wdCharacter, -1
Which put the range back inside the table, and it worked.

Steven Craig Miller
 
H

Helmut Weber

Hi Michael,

add this line:

[snip]
rng.Text = "Page "
rng.Collapse wdCollapseEnd rng.End = rng.End - 1
rng.Fields.Add rng, wdFieldPage

Otherwise your range will be in a nowhere area,
after the cell mark of the last cell in the row
and beforea the end-of-row mark.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

Hi StevenM,
As I understand it (perhaps someone will correct me), you can't add a field
to a Range, but have to add it to a document at a Range. So for example:

no, :)

Sub Test7()
Dim rtmp As Range
Set rtmp = Selection.Range
rtmp.Fields.Add rtmp, wdFieldPage
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
S

StevenM

Michael,

Also, is there a reason you need a table in your header?

You could accomplish almost the same thing with:

Sub AddHeader()
Dim oRange As Range
Set oRange =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
With oRange
.Delete
.InsertAfter "Text goes here" & vbTab & vbTab & "Page "
.Collapse wdCollapseEnd
oRange.Fields.Add Range:=oRange, Type:=wdFieldPage
End With
End Sub
 
S

StevenM

To: Helmut,

Thanks for the correction! I don't know where I picked up that idea. In
fact, in reviewing some of the code which I've written, I've added a field to
a range a number of times. So what was I thinking?

Steven Craig Miller
 

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