Inconsistent speed of long For Next loop

C

Charlie Rowe

eI am printing hundreds of envlopes on my LaserJet using Mailmerge linked to
an Excel spreadsheet. In order to keep my LaserJet from overheating, I have
put in a long For Next loop to get a 20 second pause after each envelope.
(Is there something equivalent to Excel's Application.Wait?)

Sub PrintSlowly()
Dim t As Integer, l As Long
For t = 1 To 100
ActivePrinter = "BASEMENT HP 1300"
With ActiveDocument.MailMerge
.Destination = wdSendToPrinter
.SuppressBlankLines = True
With .DataSource
.FirstRecord = t
.LastRecord = t
End With
.Execute Pause:=False
End With
For l = 1 To 600000000
Next l
Next t
End Sub

The inconsistency I don't understand is this:
Running this macro with only Word active (and thus active) is slower than if
I open Windows Task Manager and make Task Manager active.

With Task Manager active, I get a very consistent 10-second pause. With
Word active, I get a roughly 20-second pause that is not consistent (13
seconds to 25 seconds). This behavior is the same with programs other than
Task Manager open and active. Excel is not open.

I'm just curious if anyone can explain this behavior. It may have something
to do with "Now Printing Body" vs "Now Printing Page 1." When Word is
active, I see the Printer Dialog Box for many seconds with the "Now Printing
Body" as its first line, and the Task Manager shows low processor usage.
Then the first line of the Printer Dialog Box changes to "Now Printing Page
1" and the processor usage goes up to 100% or so. When Task Manager is
active, I usually do not see the "Now Printing Body" line, and the processor
usage stays pegged at 100%.

Thanks for any insight,
Charlie Rowe
 
J

Jay Freedman

Ditch the For Next loop. Put this at the top of your module, before
any Sub statement:

Private Declare Sub Sleep Lib "kernel32" ( _
ByVal dwMilliseconds As Long)

and call it like this:

Sleep 20000 ' inserts a 20 s delay

I don't have an explanation for your observations, but trying to pause
with a For Next loop is doomed to failure from the start because there
are so many factors that could make it vary.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
C

Charlie Rowe

Jay,

Thanks for the suggestion. But the same inconsistency exists with Sleep.
If another program is active, Word pauses a consistent 20 seconds with Sleep
20000. But, if Word is active, the pause is irregular.

I get the same behavior with a "Do While Timer < StartTime + 20" and
"DoEvents".

Thanks,
Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top