Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Updating page numbers on protected form (WinXP/Word2003)

    I have a protected template that has a page number field (total number of pages in document) and I need to get it to update this field on request, or better still, automatically as the figure changes. I know Word is set to update this figure only on printing, but in this form it tends not to do so even then! So my question is two-fold:

    First, I wrote the following little macro to go on a button so that the users can update the field whenever they want to:

    Private Sub cmdUpdate_Click()
    If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect Password:="****"
    Application.ScreenUpdating = False
    Selection.WholeStory
    Selection.Fields.Update
    Application.ScreenUpdating = True
    ActiveDocument.Protect wdAllowOnlyFormFields, noreset:=True, Password:="****"
    End Sub

    However, although this works perfectly when you step through it, when you just run it, it has no effect whatsoever! Why is this, does anyone know?

    Second, is there a better way to get updates to work either on demand or automatically as changes occur?

    Many thanks in advance
    Beryl M


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

    Re: Updating page numbers on protected form (WinXP/Word2003)

    Where is the NumPages field? In a header or footer, or in the body of the document? If the latter, in a protected section or in an unprotected section?

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Updating page numbers on protected form (WinXP/Word2003)

    In the main body, in a protected section - that's why I set the protection to switch off while it updates the field.
    Beryl M


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

    Re: Updating page numbers on protected form (WinXP/Word2003)

    Does this work for you?

    Private Sub cmdUpdate_Click()
    Application.ScreenUpdating = False
    Application.PrintPreview = True
    Application.PrintPreview = False
    Application.ScreenUpdating = True
    End Sub

  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Updating page numbers on protected form (WinXP/Word2003)

    Nope, unfortunately, not even when I step through it - although viewing print preview does work when I do it manually!

    <img src=/S/confused3.gif border=0 alt=confused3 width=45 height=45>
    Beryl M


  6. #6
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Updating page numbers on protected form (WinXP/Word2003)

    Just thought, if a macro works on stepping through but not when run, doesn't that sometimes indicate it's simply going past too quickly for the document to register it's happening? And isn't there a line you can put in that tells the macro to wait for [10 seconds or whatever] before continuing?

    Do you think that might help?
    Beryl M


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

    Re: Updating page numbers on protected form (WinXP/Word2003)

    Word doesn't seem to be very cooperative when it comes to updating fields in the body of the document. The following works for me, but I suspect that even if it works for you, it won't be acceptable:
    - Unprotect the template.
    - Select View | Header and Footer.
    - Insert a NUMPAGES field in one of the page headers or footers.
    - Select this field.
    - Select Insert | Bookmark...
    - Enter a name, e.g. Num, and click Add.
    - Close Header/Footer view.
    - Move to the point where you want the number of pages.
    - Press Ctrl+F9 to insert a field.
    - Type REF followed by a space and the name of the bookmark you created.
    - Press F9 to update the field.
    - Protect the document.

    The macro from my previous reply now correctly updates the REF field, even though a NUMPAGES field in the body of the document doesn't get updated.

  8. #8
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Updating page numbers on protected form (WinXP/Word2003)

    Don't be so quick to run your ideas down!! I'll have a go and see if it works in a minute, but as long as I make the text colour of the field in the header white, I see no reason why the users would even have to know it was there, do you?

    I'll let you know how I get on!
    Beryl M


  9. #9
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Updating page numbers on protected form (WinXP/Word2003)

    Okay, in principle it works, but as soon as you do ANYTHING to the numpages field, it stops auto-updating on the spot. This includes surrounding it with a bookmark, using it in a calculation (as I'm going to have to do if I use it, since the page count has to be excluding the last page!).

    I've tried using the sectionpages field instead (conveniently, all but the last page are in the same section) but that doesn't autoupdate at all, even using the original macro on a stepthrough.

    So I'm back to the original macro (or your version, with print preview) and the possibility that since it works on stepthrough but not when run, that it needs to hesitate on the way through.

    I'm sure there was a codeword that you could put in to tell it to hesitate for so many seconds then continue, wasn't there?

    Many thanks for your help so far!
    Beryl M


  10. #10
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Updating page numbers on protected form (WinXP/Word2003)

    Now that's not fair, Hans! Throwing in a comment like that without explaining what you mean!

    What's a DoEvent?!
    Beryl M


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

    Re: Updating page numbers on protected form (WinXP/Word2003)

    Edited by HansV to correct typo

    The procedure I described works for me too if I use a SectionPages field, or even a calculated field, e.g.

    { ={REF Num }-1 }

    where { } are field brackets inserted by typing Ctrl+F9, not by typing them yourself.

    BTW you could try inserting a few DoEvents instructions in the code.

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

    Re: Updating page numbers on protected form (WinXP/Word2003)

    Actually, it should have been DoEvents (I have corrected my previous reply). DoEvents is a VBA instruction that according to the VBA help,
    <hr>Yields execution so that the operating system can process other events<hr>

  13. #13
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Updating page numbers on protected form (WinXP/Word2003)

    Okay, I've read the VBA Help on the subject, and you know something?! I still don't have the faintest idea what a DoEvents does!

    <img src=/S/confused.gif border=0 alt=confused width=15 height=20> <img src=/S/brickwall.gif border=0 alt=brickwall width=25 height=15> <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>

    Help?
    Beryl M


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

    Re: Updating page numbers on protected form (WinXP/Word2003)

    The following is not intended to be technically accurate.

    Windows applications are event-driven. Each time something happens - you click or move the mouse, you press a key on the keyboard, a window is resized, the contents of a window change, etc., etc. - an "event" occurs, a sort of cry for attention: "yoohoo, Beryl clicked here". Like good British citizens, the events form a queue: the event queue. The operating system processes the events in an orderly fashion, assigning some processor capacity to each in turn, according to certain rules.
    Event processing can be time-consuming, it can seriously slow down the execution of your VBA macros. Therefore, VBA tells Windows to reduce event processing while a macro is being executed. In extreme cases, this can make the computer unresponsive to the mouse and keyboard and the screen slow to update. The DoEvents instruction tells the operating system that it may take some time to process events in the event queue before code execution continues.

  15. #15
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Updating page numbers on protected form (WinXP/Word2003)

    Thanks for that Hans.

    I've been trying to find the code that lets you tell the macro to stop for X seconds before restarting, but I can't find it anywhere! I found application.ontime, but that only lets you specify another macro to run after the specified time lapse (and I can't get that to work, either)!

    Could you please tell me what this code is, please? It is horrendously difficult to find a function when you only know it exists, but not what it's called!
    Beryl M


Page 1 of 2 12 LastLast

Posting Permissions

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