Escape Sequence in VB

How do you issue an Escape Sequence in VB5 to control a printer ? I am trying to issue   ESC "d" 0   to control a paper cutter in a small ticket printer. I have tried   printer.print chr$(27)+chr$(100)+chr$(48)  but the characters "d0" actually got printed out which shouldn't have been and the ESC control code have not functioned. Can anyone help ??
Who is Participating?
tureConnect With a Mentor Commented:

Try something like this:

  Open Printer.Port For Binary As 1
    Put #1, , Chr(27) & "d0"
  Close 1

Ture Magnusson
Karlstad, Sweden
tinsonAuthor Commented:
Hi Ture,
Thanks for your reply!
The code works fine on its own, it does the job, no problems. However, if I insert it into my code as follows, I get "Run time error 75 - Path/File access error", I can't see what the problem is, can you help ?

My Code:

Private Sub Command1_Click()

' Print Text
Printer.ScaleMode = 6
Printer.FontName = "SwitzerlandNarrow"
Printer.FontItalic = False: Printer.FontBold = False
Printer.FontSize = 10
Printer.Print "test"

' Cut Paper
Open Printer.Port For Binary As 1
   Put #1, , Chr(27) & "d0"
Close 1

End Sub

I'm not sure... Perhaps it's a timing error - your previous interaction with the printer is not quite finished.

If you add a msgbox just before 'Cut Paper', so that you can make the procedure wait a little - Does that help?

Then you can try something like this to keep trying until the port is can be opened.

  f = FreeFile
    Open Printer.Port For Binary As f
  Loop Until FreeFile <> f
  Put #f, , Chr(27) & "d0"
  Close f

tinsonAuthor Commented:
Hi Ture
Thanks for your prompt reply.
I think you are right, it is a timing error.
I have done what you suggested, but still no good because the printer is slow in responding to the printing instructions; It works only if the user issue the cutting ESC sequence via a MessageBox after everything is printed. But problems occured when I confirm 'OK' in the MessageBox  to trigger the cutting before the actual printing is finished.
What is the VB code for checking the printer has finished printing ? If I can first check the printer has finished printing, than issue Cutting ESC instruction in the Code, I think the problem can be solved.  What do you think ?

Thanks yet again for your helpful advice.


I see that you have posted a new question regarding how to check if the printer has finished printing. That is good, because I haven't really done this before. Visual Basic works nicely when you depend on printer drivers, but when you wish to send control codes directly to the printer you need some special procedures.

I have found some information on 'sending raw data' to a printer using Windows API calls and Visual Basic. I'm not good at API calls, but I really think that it is the way to solve your printing problem. Read about it at:

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.