Entering Page fields mixed with text 
Author Message
 Entering Page fields mixed with text

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



Sun, 11 May 2003 03:00:00 GMT  
 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



Sun, 11 May 2003 03:00:00 GMT  
 Entering Page fields mixed with text
Hi Mike,

Skip the collapse commands in your code.
Should work.

Krgrds,
Perry



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



Sat, 07 Jun 2003 06:05:33 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. calculate a text field from two previously entered text fields

2. Entering text in form field of locked document pushes text on same line

3. Formatting mixed text/fields in a report

4. Mixing Text w/ Numeric Fields

5. Mixing tabs, fields, text in a header

6. entering data in a field that would enter correct data in another field automatically

7. mixing integer and text in a text box

8. The ability to enter Hyperlink in a data access page text box

9. Excel freezes when dealing with entering mixed alphanumeric chars in a cell

10. Entering unlimited text in fields


 
Powered by phpBB® Forum Software © phpBB Group