Results 1 to 15 of 15
  1. #1
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    NumPages doesn't update (WinXP/Office2003 NL)

    I have a document with a frame that contains a few lines with FormFields and Fields. One of those lines is "page X of Y". The primary header of the doc also contains a frame with a line "page X of Y". In both cases Y is the NumPages field. When there are more than 1 pages in the doc, the Y on all pages is updated, EXCEPT on page 1. See attached file, which is very much stripped down. (If you wonder about some other fields, these are IF fields, presenting text in Dutch or English, depending on the value of CustomDocumentProperty "Taal" (= Dutch for "Language").)
    Whatever I do, in Word itself or through VBA code, I cannot get this field on page 1 to update, unless this field, and only this field(!), is selected and then F9 is pressed or Selection.Fields(1).Update is executed. What am I doing wrong? Can anyone shed some light at this?
    Attached Files Attached Files

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    The NUMPAGES field on page 2 and subsequent pages is in a frame in the page header. All fields in the page header are automatically updated when you print or print preview the document.
    The NUMPAGES field on page 1 is in a frame in the body. Not all fields in such a frame are updated automatically. Unfortunately, you cannot move this frame "as is" to the header, since it contains form fields.
    One solution is to hijack the print commands, and to update all fields in all storyranges - see WD2000: How to Automatically Update Fields.
    Another one is to create a userform to collect information, and use this to place information into the frame on page 1, instead of using form fields. You can then move the frame into the page header.

  3. #3
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    Hans, thanks for the suggestions. I tried the code in the article you pointed to, but that also doesn't work. I kinda implied that in my first post but obviously I wasn't clear enough. Sorry. So let me expand a little. Just for testing I wrote a little Sub that iterates through all fields of the frame and only tries to update the NumPages field:

    Sub UpdateFields
    Dim fld As Field
    For Each fld in ActiveDocument.Frames(1).Range.Fields
    If fld.Type = wdFieldNumPages Then
    fld.Update
    End If
    Next fld
    End Sub

    Executing this code does not update the NumPages field. But singlestepping through the code does! I just don't understand why...
    As for your suggestion to create a userform: I'm not allowed to do that. The document is based on a mandatory company "housestyle" template that we have to use "as is".

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    Sorry, I hadn't tested the code in your document. I see now that it doesn't work.

    Try the following:
    Select the NUMPAGES field in the header on page 2.
    Select Insert | Bookmark, and specify a (unique) name for the bookmark, e.g. NumPag.
    Close the header, and go to the frame on page 1.
    Delete the NUMPAGES field, and change it to a field referring to the NumPag bookmark you just created.
    The code still won't update it, but printing/print previewing should update it.

  5. #5
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    OK, that works for this document. And adding pages leads to updated and correct values of "NumPages" on page 1. Deleting pages also works OK, unless there's only 1 page left. In that case after updating or with printpreview it says 1 of 2....
    So, unless somebody has another brainwave, I'm stuck. <img src=/S/sad.gif border=0 alt=sad width=15 height=15>

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    Perhaps <!profile=macropod>macropod<!/profile> will come up with something. He can make fields do things I wouldn't have thought possible.

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    Hi Jan:
    I'm not sure why it won't update correctly after using Print Preview. It does for me using Print Preview (Word 2003). However, I tried using a SET & REF field in place of the Numpages field & it seems to work. See if this helps.

    Edited to add: Some more checking on a sample document I made. It seems that fields are updating one at a time. i.e. I can put 3 lines on the first page that say

    {page} of {numpages}
    {page} of {numpages}
    {page} of {numpages}

    select all & then update fields. I have to update three times, with each line updating after pressing F9. Ah ha!! See <!mskb=835227>Microsoft Knowledge Base Article 835227<!/mskb>
    Cheers,
    Attached Files Attached Files

  8. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    Hi Jan,

    Try the following code. I think you'll find it works!

    Cheers

    Sub UpdateNumPages()
    Dim oRange As Word.Range
    Dim oFld As Field
    For Each oRange In ActiveDocument.StoryRanges
    Do
    For Each oFld In oRange.Fields
    If oFld.Type = wdFieldNumPages Then oFld.Update
    Next
    Set oRange = oRange.NextStoryRange
    Loop Until oRange Is Nothing
    Next
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. #9
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    Phil & macropod, thanks for your suggestions. I will try them later on and report back.

  10. #10
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    Still no joy. I put a Beep statement just before oFld.Update and executed the Sub several times in a row. Each time the Beep is heard, but the field just doesn't update. <img src=/S/sad.gif border=0 alt=sad width=15 height=15>

  11. #11
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    When you tried, did your code work with the document protection (for formfields) turned on? If I try that, the field isn't updated. If I turn protection off manually, then your code works. So I put calls to my standard ProtOff and ProtOn sub's (see below) at the start and end of your code, but then the field wasn't updated... <img src=/S/confused.gif border=0 alt=confused width=15 height=20>

    <font face="Georgia"> Public Sub ProtOff(Optional blnNull As Boolean)
    'Optional argument prevents that Sub is visible in Tools | Macro | Macro list
    If Documents.Count < 1 Then Exit Sub
    If ActiveDocument.Protection.Type <> wdNoProtection Then
    ActiveDocument.Unprotect Password:=" <img src=/w3timages/censored.gif alt=censored border=0> "
    End If
    End Sub

    Public Sub ProtOn(Optional blnNull As Boolean)
    'Optional argument prevents that Sub is visible in Tools | Macro | Macro list
    If Documents.Count < 1 Then Exit Sub
    If ActiveDocument.Protection.Type = wdNoProtection Then
    ActiveDocument.Protect Password:=" <img src=/w3timages/censored.gif alt=censored border=0> ", NoReset:=True, Type:=wdAllowOnlyFormFields
    End If
    End Sub </font face=georgia>

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    What happens if your boss discovers that you use <img src=/w3timages/censored.gif alt=censored border=0> as password? <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>

  13. #13
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    Hi Jan,

    I ran the code on your protected document and it worked fine.

    When I ran it with your unprotect/protect code, I got an error on the lines:
    If ActiveDocument.Protection.Type <> wdNoProtection Then
    which should be:
    If ActiveDocument.ProtectionType <> wdNoProtection Then
    When I correct that, the code still works. Copy with code attached.

    Cheers
    Attached Files Attached Files
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  14. #14
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    He'll try to type that in, can't find the right key and then call me for help... <img src=/S/innocent.gif border=0 alt=innocent width=20 height=20>

  15. #15
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: NumPages doesn't update (WinXP/Office2003 NL)

    <img src=/S/stupidme.gif border=0 alt=stupidme width=30 height=30>
    Sometimes I'm not on my own work-PC when I come into the Lounge and then I cannot upload. So I typed it in and made a typo; in reality I use the same syntax as you did.
    In the meantime, I discovered some other strange behaviour regarding my global code-only-template, so I'll investigate the possibillity of corruption.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •