How to Add field codes to table in footer?

J

John Henry

In using VBA I need to add a 1 row, 2 column int the footer. In the left
column I need to display the current date and in the right column display
page of pages. I can create the table, display the current date, but I
cannot seem to add the page of pages to the right column.

The error message I receive is: This command is not available.
What I am trying to do is grab the cell as a range, then use that range to
get the field result. I have used the activedocument.fields.add method, but
it displays the the results right on document and not where I need it.

Does anyone have any suggestions?

Here is my code:

Dim myRange As Range
Dim strPageFld As String, strTotPageFld As String

With ActiveDocument
Set myRange = .Sections(1).Footers(wdHeaderFooterPrimary).Range
.Tables.Add Range:=myRange, NumRows:=1, NumColumns:=2
myRange.Tables(1).Cell(1, 1).Range.ParagraphFormat.Alignment =
wdAlignParagraphLeft
myRange.Tables(1).Cell(1, 1).Range.Text = Format(Date, "MMMM dd,
yyyy")

Set myRange = myRange.Tables(1).Cell(1, 2).Range
myRange.ParagraphFormat.Alignment = wdAlignParagraphRight
strPageFld = myRange.Fields.Add(myRange, wdFieldPage).Result
strTotPageFld = myRange.Fields.Add(myRange, wdFieldNumPages).Result

myRange.Text = strPageFld & " of " & strTotPageFld

End With
 
D

Doug Robbins - Word MVP

myRange.Tables(1).Cell(1, 2).Range.Select
Selection.TypeText "Page x of y"
Selection.Range.InsertAutoText


--
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
 
J

John Henry

Thanks for the response.
I am at home today with a sick daughter, so I will let you know tomorrow as
to my success or sorrow.
 
J

John Henry

Worked like a charm. Thank you very much.


Doug Robbins - Word MVP said:
myRange.Tables(1).Cell(1, 2).Range.Select
Selection.TypeText "Page x of y"
Selection.Range.InsertAutoText


--
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
 

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