
Entering Page fields mixed with text
Hi Mike,
Once the first field is added, your range object will be directed to the
startposition of the field. If you're not sure where the range points to,
you can use the select method to see what you're doing (wdrange.select)
-------------------------------------------------------------------------
'enter page field
.Fields.Add Range:=wdRange, Type:=wdFieldPage
'Reset range to end of paragraph
.MoveStartUntil cset:=Chr(13), Count:=wdForward
-------------------------------------------------------------------------
If you want to write the fields directly into the primary header (of the
first section in this example) without having to select it first, try:
-------------------------------------------------------------------------
Sub SetPageNoFields()
Dim oHeader As Word.HeaderFooter
Dim oRange As Word.Range
Set oHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
Set oRange = oHeader.Range
With oRange
.Text = "Page "
.Collapse wdCollapseEnd
.Fields.Add Range:=oRange, Type:=wdFieldPage
.MoveStartUntil cset:=Chr(13), Count:=wdForward
.Text = " of "
.Collapse wdCollapseEnd
.Fields.Add Range:=oRange, Type:=wdFieldNumPages
End With
Set oRange = Nothing
Set oHeader = Nothing
End Sub
-------------------------------------------------------------------------
Hope this helps,
regards,
Astrid
For direct access to all Microsoft newsgroups:
Quote:
> I am attempting to enter one of several different formats of page
numbering
> in a header and have encountered a problem mixing Fields with text.
> The example code below attempts to enter the "Page 1 of 10" format.
> When recording a macro, text and fields are entered in the correct
sequence.
> In VBA, despite using the Collapse method, text cannot be entered after a
> field and a second field cannot be entered after the first entered field.
> The resulting sequence is "Page of 10 1" (except there would be no space
> between the total pages and the current page).
> There appears to be an issue which I do not understand concerning the
range
> object and field codes. Could anybody throw some light on this?
> Regards,
> Mike Barton
> Thomson Financials
> Sample code:
> -------------------------------------------------
> Sub SetPageNoFields()
> Dim intFormat As Integer
> Dim wdRange As Word.Range
> Set wdRange = Selection.Range
> With wdRange
> .Collapse wdCollapseStart
> 'enter "Page" Text
> .Text = "Page "
> .Collapse wdCollapseEnd
> 'enter page field
> .Fields.Add Range:=wdRange, Type:=wdFieldPage
> .Collapse wdCollapseEnd
> 'enter "/"
> .Text = " of "
> .Collapse wdCollapseEnd
> 'add total pages field
> .Fields.Add Range:=wdRange, Type:=wdFieldNumPages
> End With
> End Sub