From: Michael Gordon Michael on
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!
From: StevenM on
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



"Michael Gordon" wrote:

> 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!
From: Michael Gordon on
That worked! Thanks!

"StevenM" wrote:

> 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
>
>
>
> "Michael Gordon" wrote:
>
> > 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!
From: Helmut Weber on
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
From: Helmut Weber on
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