PDA

View Full Version : Printing in a Loop with a Sleep



Adamsons
06-18-2013, 06:22 AM
Hi all

I have created (borrowed and modified) a macro that will print a serial number on a word template and prints one sheet for each serial number.

This works well, it asks how many to print and remembers the last number. However, when you print the sheets get muddled/shuffled in the print queue which is an issue when you print 100's.

I wanted to add a Sleep command to make the code print one, sleep for x seconds and then print another to allow the printer (as I thought it was) to catch up.

However, when you look closely at what it is doing, it seems to part spool for each iteration and then when it has looped through them all it finishes off the spooling and as such my sleep command does not work.

Any ideas?

The code I am using is:


Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub PrintNumbers()
'
' PrintNumbers Macro
'
Dim Message As String, Title As String, Default As String, NumCopies As Long
Dim Rng1 As Range
' Set prompt.
Message = "Enter the number of copies that you want to print"
' Set title.
Title = "Print"
' Set default.
Default = "1"
' Display message, title, and default value.
NumCopies = Val(InputBox(Message, Title, Default))
SerialNumber = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "SerialNumber")
If SerialNumber = "" Then
SerialNumber = 1
End If
Set Rng1 = ActiveDocument.Bookmarks("SerialNumber").Range
Counter = 0

While Counter < NumCopies
Rng1.Delete
Rng1.Text = SerialNumber
ActiveDocument.PrintOut
Sleep 5000
SerialNumber = SerialNumber + 1
Counter = Counter + 1

Wend
'Save the next number back to the Settings.txt file ready for the next use.
System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
"SerialNumber") = SerialNumber
'Recreate the bookmark ready for the next use.
With ActiveDocument.Bookmarks
.Add Name:="SerialNumber", Range:=Rng1
End With
ActiveDocument.Save
End Sub





It does not seem to matter where I put the sleep command.

Any help would be appreciated

Thanks

Matt Houldsworth

fumei
06-19-2013, 05:04 PM
Perhaps Do Events instead.

Adamsons
06-20-2013, 01:14 AM
Perhaps Do Events instead.

Hi

I am no VBA expert... php is my thing! Are you saying move the print part out to be triggered as an event?

Matt