• NumPages doesn’t update (WinXP/Office2003 NL)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » NumPages doesn’t update (WinXP/Office2003 NL)

    Author
    Topic
    #423785

    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?

    Viewing 0 reply threads
    Author
    Replies
    • #971105

      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.

      • #971119

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

        • #971121

          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.

          • #971136

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

            • #971138

              Perhaps macropod will come up with something. He can make fields do things I wouldn’t have thought possible.

            • #971280

              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 Microsoft Knowledge Base Article 835227
              Cheers,

            • #971304

              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
              [Fmr MS MVP - Word]

            • #971322

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

            • #971385

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

            • #971398

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

              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:=” censored
              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:=" censored “, NoReset:=True, Type:=wdAllowOnlyFormFields
              End If
              End Sub

            • #971429

              What happens if your boss discovers that you use censored as password? evilgrin

            • #971618

              He’ll try to type that in, can’t find the right key and then call me for help… innocent

            • #971540

              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

              Cheers,
              Paul Edstein
              [Fmr MS MVP - Word]

            • #971620

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

    Viewing 0 reply threads
    Reply To: NumPages doesn’t update (WinXP/Office2003 NL)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: