Printing labels on a dot matrix printer - what does this code do

L

Laurence Lombard

In my search for a method to print labels on a dot matrix printer from Excel
I found the post http://www.theofficeexperts.com/for...hread.php?t=831 which
gave a fine solution, but there is a bit of code which I do not understand.
(My VBA is self taught via trial and error).

PauseTime = 2 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop

..
The code writes to PRINTLABEL.TXT and then executes a PRINTLABEL.BAT (which
contains the line "copy PRINTLABEL.TXT lpt1") for the number of times one
wants the label printed. I suspect the code above slows down the execution
so that the BAT file does not run faster than the printer can print.

Thanks
Laurence

PS I have looked at David McRitchies suggestions of using Word and
mailmerge, but I prefer this methos as everything is done in Excel (my
preference).


An abbreviated version of the code follows for details sake. It works well
and I thought other that use this forum might also find it useful.

----------------------------------------------------------------------------
--------------
Private Sub PRINTLABEL_Click()
On Error GoTo Err_PRINTLABEL_Click

Dim Message, Title
Dim NUMCOPIES As Integer
Dim PauseTime, Start, Finish, TotalTime
Dim PRINTLINE1 As Variant
Dim PRINTLINE2 As Variant
Dim PRINTLINE3 As Variant
Dim PRINTLINE4 As Variant
Dim PRINTLINE5 As Variant
Dim PRINTLINE6 As Variant
Dim RetVal

Open "c:\PRINTLABEL.TXT" For Output Shared As #1


PRINTLINE1 = [Mr/Mrs] & " " & [ContactFirstName] & " " & [ContactLastName]
PRINTLINE2 = [CompanyName]
PRINTLINE3 = [Address1]
PRINTLINE4 = [Address2]
PRINTLINE5 = [POB]
PRINTLINE6 = [City] & ", " & [StateOrProvince] & " " & [PostalCode]


Print #1, " "
Print #1, (PRINTLINE1)
Print #1, (PRINTLINE2)
Print #1, (PRINTLINE3)
Print #1, (PRINTLINE6)
Print #1, " "
Close #1

NUMCOPY:

PauseTime = 2 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop

Message = "Enter the number of copies"
NUMCOPIES = InputBox(Message, Title, NUMCOPIES + 1)
For counter = 1 To NUMCOPIES

PauseTime = 2 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop


RetVal = Shell("C:\LABPRT.BAT") ' PRINTS LABEL.
Next counter

Exit_PRINTLABEL_Click:
Exit Sub

Err_PRINTLABEL_Click:
MsgBox Err.Description
Resume Exit_PRINTLABEL_Click

End Sub
 
R

raypayette

This is old code, probably a QuickBasic legacy!

It seems that the printer needed some delay to process the matter.
If your printer is recognized by Windows® I strongly suggest that you
let Windows handle it. You might need to update the driver by going to
the manufacturer's web page.

I would rework everything from Excel.
 
L

Laurence Lombard

Thanks for the reply. I reply to the group and directly as I am not sure
whether you would notice replies to "old" posts (at the rate postings are
made to this group they age very quickly). I did notice that if I want to
print many labels only about maximum 7 or 8 get printed, so I assumed this
bit of code was included to handle that.

I worked around it by writing all the labels to the text file and then
copying the text file to lpt1 once only. This works well enough. The only
hitch is that if the printer messes up a label (my printer does that
sometimes - I think it is a mechanical fault) and I switch it off, realign
the labels and switch on, the printing goes on where it left off - with the
alignment most likely out. I would like to have the printer queue cleared if
the printer stops or is switched off. Fortunately the printer is behaving
well and I print in smaller batches of 20.

Laurence
 

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