Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    Dec 2003
    Location
    Sydney, New South Wales, Australia
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Slowing down a macro (Word 2002 SP3)

    Is there any magic line of code I can use to slow down a VBA macro in Word 2002? Sounds like a silly question, I know-- everybody else wants their macros to run faster-- but the macro I'm working with (which goes through a Word table reformatting it with shading, font sizes, borders, merging cells, etc.) does not run consistently in normal mode. However, in debug manual step-through (F8) it runs ok. I'm wondering therefore if the macro running at full speed is somehow causing it to malfunction. Any suggestions gratefully accepted!
    Mal

  2. #2
    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: Slowing down a macro (Word 2002 SP3)

    The Windows API offers a function named Sleep which causes the program to simply pause execution for as long as you specify. Using Sleep requires two parts. At the top of a code module, you put this declaration:

    <pre>'Declare Sleep API
    Private Declare Sub Sleep Lib "kernel32" (ByVal nMilliseconds As Long)</pre>


    Then, in your code in that module, you call the procedure this way:

    <pre>Sleep 500 'wait 1/2 sec before trying again</pre>


    (In my case, that's in a loop waiting for an object to signal that it is ready to go.)

    Hope this helps.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Slowing down a macro (Word 2002 SP3)

    > However, in debug manual step-through (F8) it runs ok.
    I remember having this problem. Very annoying. Runs in Step mode, won't run unattended.
    I forget the actual cause, but it was related to something I had in my program code, not a timing problem. And hence a delay loop didn't help.
    I had to master the subtle art of looping backwards through colelctions and/or storing details of what I was doing in an array, then processing via an array.

    Typical sort of hazard for me: Trying to modify every Field in a collection by:<pre>For Each fld in doc.Fields
    da-de-dah some operation on a field
    Next fld</pre>


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

    Re: Slowing down a macro (Word 2002 SP3)

    You could also try inserting DoEvents statements in your code, especially within loops.

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Slowing down a macro (Word 2002 SP3)

    > Sleep 500 'wait 1/2 sec before trying again
    I hate it that this works.

    I remembered your post today and implemented it.
    I am looping through folders, pre-processing by testing documents to see if they can be opened without baulking. Those that baulk get their names written to an "exclusion" file and aren't invited again.
    I noticed that a high proportion of normal documents were excluded.
    Single-stepping indicates no problem with the documents in question.
    DoEvents don't cut it on this 2GB ram 2GHz laptop (WinXP/SP2 Word2000).
    Sleep 500 circumvents the problem. (Sleep 50 may work as well).
    The extra cost in idling repays the cost of (a) restarting the job ([img]/forums/images/smilies/cool.gif[/img] having too many documents excluded and manually processing those documents at the end of the automated run.

    I love you, but I still hate it that this works.

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Slowing down a macro (Word 2002 SP3)

    That's not a speed problem, it's a program logic problem.

    The details of how to fix this vary greatly depending on your code.

    REmember that by default Forms are modeless and the code after the Form will run before the form completes.
    This is likely the biggest cause of the problem you are seeing.

    If this be the cause. make the form modal.

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Slowing down a macro (Word 2002 SP3)

    Have you explored table styles in Word 2002. You will find that shading, borders, font sizes can all be controlled by a Table Style and applied far more easily this way.

    You will still need code to merge cells and any other changes that the Table Style doesn't control but the processing should be considerably less.

    Note: If you need the macro to work on versions earlier than Word 2002 then you will have to do this the labour-intensive way (or using a Table Autoformat)
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  8. #8
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Slowing down a macro (Word 2002 SP3)

    I have encountered such problems with AutoFit: That is usually done in a background process (as the name implies: AutoFitBehaviour), not immediately.
    Most times it helps to stick in one or two "ActiveDocument.Repaginate", since Word has to do the AutoFit to determine the pagination.
    May be faster and more reliable than putting Word to sleep...

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

Posting Permissions

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