Results 1 to 11 of 11
  1. #1
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    DocumentChange infinite loop (Word 2003)

    I have a word template in my startup folder. This has a DocumentChange event which fires whenever the focus switches to a different word document. It is used to modify a number of application specific toolbars.

    The code called from the event changes CustomizationContext to the attached template and to the Global template containing the event code, and enables / disables various toolbar controls. It manipulates the .Saved property of the templates so that it doesn't "dirty" them with it's changes.

    This code works perfectly on Word 2000 and Word 2002, but on Word 2003 it generates an infinite loop whenever the user vists the header of a document. Single stepping the code I can see that the code runs to completion and then the event immediately fires again. This is not a recursion - it does complete each time and exits at the final End Sub. It is almost as though there is an event queue waiting for the macro to exit. It only happens if the user double clicks in the header of a document (or possibly a footer, I haven't checked this).

    I tried setting a boolean variable at the beginning of the event routine and immediately exiting if I find it true, but since this is not recursion it didn't help. I also tried inserting a DoEvents before exiting to see if that helped, but it still waits till the Exit Sub before starting again! I even tried disabling events by putting
    <code>Set AppEvents.App = Nothing
    Set AppEvents.App = Word.Application</code>
    at the beginning and end of the event code, but this didn't help either.

    Any suggestions?

    StuartR

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

    Re: DocumentChange infinite loop (Word 2003)

    Can you do something with <code>Selection.Information(wdInHeaderFooter)</code> ? It is True if the selection/insertion point is in a header or footer.

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: DocumentChange infinite loop (Word 2003)

    I can certainly avoid the infinite loop by including
    <code>If Selection.Information(wdInHeaderFooter) Then Exit Sub</code>
    Unfortunately this also means that I end up with my toolbar controls incorrectly enabled / disabled.

    StuartR

  4. #4
    3 Star Lounger Jim Cone's Avatar
    Join Date
    Feb 2002
    Location
    Portland, Oregon, USA
    Posts
    238
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: DocumentChange infinite loop (Word 2003)

    Stuart,

    The single word "End" just before Exit Sub ought to do the trick.
    Are you sure the event is not being called elsewhere?
    Also,maybe it is an "improvement" MS added to the 2003 version. The perpetual event?

    Regards,
    Jim Cone
    San Francisco, CA

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: DocumentChange infinite loop (Word 2003)

    Putting an End just before the Exit Sub fixed the infinite recursion - but at an even greater cost than Hans solution. This reset the event handler so it was never called again.

    StuartR

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

    Re: DocumentChange infinite loop (Word 2003)

    Word needs an Application.EnableEvents property, like Excel, but alas...

  7. #7
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: DocumentChange infinite loop (Word 2003)

    Exactly what I need. Or a way to flush pending events before I return. I was really hopeful that DoEvents and a static boolean variable would do the trick.

    I still have to decide how to manage my toolbars. Maybe I will check for the version of Word and exit if we have Word 2003 and are in a header. It won't do exactly the right thing, but might be acceptable to the users (although I suspect I will get lots of complaints).

    StuartR

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: DocumentChange infinite loop (Word 2003)

    Can you use the New and Open events to customize the toolbars associated with the attached template? I guess this depends whether and when you know what customizations are needed. Whether triggered by new/open or something else, it seems in theory that those changes should "stick" between changes of focus among open documents. Perhaps I put too much faith in the claims of the documentation, and you adopted DocumentChange due to Word's limitations. <img src=/S/smile.gif border=0 alt=smile width=15 height=15> Still, perhaps worth revisiting for Word 2003.

  9. #9
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: DocumentChange infinite loop (Word 2003)

    This is a good idea, but some of the toolbar changes are to set "Toggle" state buttons to the correct state for the current document, and the user could have multiple documents open based on the same template.

    StuartR

  10. #10
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: DocumentChange infinite loop (Word 2003)

    I now have a fix, or possibly a workaround, but I totally fail to understand what was going on!

    My DocumentChange event called a routine that (amongst other things) checked the KeepWithNext attribute of all Paragraphs in the current selection. Anytime my code looks at the KeepWithNext property of a paragraph in a header it appears to trigger a DocumentChange event!

    Changing the check from
    <code>
    if paraNext.KeepWithNext</code>

    to
    <code>
    if paraNext.Format.KeepWithNext</code>

    made the problem go away.

    I don't even know what the difference between these two is, I always thought they were two different ways to look at the same thing!

    Bemused of London

  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: DocumentChange infinite loop (Word 2003)

    Hi Stuart,

    The DocumentChange event also loops in other situations. See my <post#=360433>post 360433</post#>.

Posting Permissions

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