Monday, April 12, 2010

Dot Matrix Printing - Part 2

How to use dot matrix printing in VB 6.0


In the first part of this tutorial we discussed the basics of printing in Vb 6.0. We discussed the lack of direct text

printing support in Windows. Now we will see the Win32 API functions used to print / emulate Dos mode text printing using

Visual Basic 6.0

First declare the following functions and types in your code. Better use a separate module to put these declarations.

Private Type DOCINFO
    pDocname As String
    pOutputFile As String
    pDatatype As String
End Type
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As

Long, ByVal pDefault As Long) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As

Long, pDocInfo As DOCINFO) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Function WritePrinter Lib "winspool.drv" (ByVal hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten

As Long) As Long

Thats all about declarations.

Then write the following function

Public Function RawPrint(Optional FileToPrint As String) As Boolean
   
    Dim lhPrinter As Long
    Dim lReturn As Long
    Dim lpcWritten As Long
    Dim lDoc As Long
    Dim sWrittenData As String
    Dim MyDocInfo As DOCINFO
    If Printers.Count <= 0 Then
        MsgBox "No printer(s) found!"
        Exit Function
    End If
    OpenTextFile ' Write a function to open the text file to be printed.
   
    lReturn = OpenPrinter(Printer.DeviceName, lhPrinter, 0)
    If lReturn = 0 Then
        MsgBox "Printing failed!"
        Exit Function
    End If
    MyDocInfo.pDocname = "Ultimate Solutions"
    MyDocInfo.pOutputFile = vbNullString
    MyDocInfo.pDatatype = "RAW"
    lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
    Call StartPagePrinter(lhPrinter)
    sWrittenData = "Test printing"
    While Not EOF(mRptPrint)
        Line Input #mRptPrint, sWrittenData
        sWrittenData = sWrittenData & vbLf
        lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, Len(sWrittenData), lpcWritten)
    Wend
   
    lReturn = EndPagePrinter(lhPrinter)
    lReturn = EndDocPrinter(lhPrinter)
    lReturn = ClosePrinter(lhPrinter)
    Close #mRptPrint
End Function

This function takes a text file as parameter. It uses win32 printer functions declared above to print the text file line by

line. Because of RAW printing in text mode. The text will have standard size and will use standard printer font's like

courier, sans serif, etc. The bold, italic, underline as we do normally in a Ms-word file requires a bit of extra coding.

This requires control codes to be sent to the Dot Matrix printers.

You can also set CPI or characters per Inch. This will tell the printer to set the font width appropriately. You can also set

the paper size in inches or lines. A typical A4 size paper can hold about 72 lines.

To start bold printing use ESC + G (capital G) that is ASCII 27 + G, the actual code in VB is chr(27) +"M". This will the

tell the printer to start printing in bold letters. To stop bold printing just send chr(27) + "H"

sending means the WritePrinter function that sends output to printer. In the above printing function the sWrittenData

parameter should be set to this value (chr(27)+"H") for stop bold printing.

To set the characters per Inch,


To print 12 characters per Inch use Chr(27) + "g"
To print 17 characters per inch use Chr(27) + Chr(15) in VB
To Print 20 characters per inch use Chr(27) + "M" + Chr(27) + Chr(15)

To set paper size to 6 inch use Chr(27) + "C" + Chr(35)
For 8 inches use Chr(27) + "C" + Chr(47)
For 12 inch use Chr(27) + "C" + Chr(71)

For direct printing you can use another method instead of Win32 API. Just use the file IO in VB 6.0. Just open the printer port as if it were a file.

For file opening you will use the syntax.

Open 'filename' for output as 'filenumber'

This is the standard VB 6.0 syntax for opening a file for writing.

Commonly Dot matrix printers are connected to LPT ports. In most cases this will be LPT1:

so the following syntax can be used.

Open "LPT1:" for output as 1

to get the next available filenumber use FreeFile function.

Then use the Print FileNumber, Text syntax to print.

For example, Print 1, "HelloWorld!"

Then close the file using 'Close Filenumber' syntax, For example Close 1. Immediately after closing the file the printer will start printer whatever you printed using Print syntax. Thats all about Dot Matrix Fast printing using VB 6.0!