Microsoft KB Archive/298141

= How To Send a PCL or PostScript File Directly to a Printer with Visual Basic =

Article ID: 298141

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q298141



SUMMARY
This article demonstrates how to bypass the printer driver and send a PCL or PostScript file directly to a printer from Visual Basic.



Step-by-Step Example
 Start a new Standard EXE project in Visual Basic. Form1 is created by default. Place two Command Buttons, Command1 and Command2, onto Form1.  Add the following code to the code window of Form1: Option Explicit ' Modify FILE_NAME to point to a valid path and file name on your system. Const FILE_NAME = &quot;C:\TEST.PRN&quot;

Private Type DOCINFO pDocName As String pOutputFile As String pDatatype As String End Type

Private Declare Function ClosePrinter Lib &quot;winspool.drv&quot; (ByVal _ hPrinter As Long) As Long Private Declare Function EndDocPrinter Lib &quot;winspool.drv&quot; (ByVal _ hPrinter As Long) As Long Private Declare Function EndPagePrinter Lib &quot;winspool.drv&quot; (ByVal _ hPrinter As Long) As Long Private Declare Function OpenPrinter Lib &quot;winspool.drv&quot; Alias _ &quot;OpenPrinterA&quot; (ByVal pPrinterName As String, phPrinter As Long, _ ByVal pDefault As Long) As Long Private Declare Function StartDocPrinter Lib &quot;winspool.drv&quot; Alias _ &quot;StartDocPrinterA&quot; (ByVal hPrinter As Long, ByVal Level As Long, _ pDocInfo As DOCINFO) As Long Private Declare Function StartPagePrinter Lib &quot;winspool.drv&quot; (ByVal _ hPrinter As Long) As Long Private Declare Function WritePrinter Lib &quot;winspool.drv&quot; (ByVal _ hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, _  pcWritten As Long) As Long

Private Sub Command1_Click Dim lhPrinter As Long Dim lReturn As Long Dim lpcWritten As Long Dim lDoc As Long Dim sWrittenData As String Dim MyDocInfo As DOCINFO lReturn = OpenPrinter(Printer.DeviceName, lhPrinter, 0) If lReturn = 0 Then MsgBox &quot;The Printer &quot; & Printer.DeviceName & &quot; could not be opened!&quot; Exit Sub End If MyDocInfo.pDocName = &quot;My Document&quot; MyDocInfo.pOutputFile = vbNullString MyDocInfo.pDatatype = vbNullString lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo) Call StartPagePrinter(lhPrinter) sWrittenData = &quot;How's that for Magic !!!!&quot; & vbFormFeed lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _    Len(sWrittenData), lpcWritten) lReturn = EndPagePrinter(lhPrinter) lReturn = EndDocPrinter(lhPrinter) lReturn = ClosePrinter(lhPrinter) End Sub

Private Sub Command2_Click Dim lhPrinter As Long Dim lReturn As Long Dim lpcWritten As Long Dim lDoc As Long Dim sWrittenData As String Dim MyDocInfo As DOCINFO Dim NumLoops As Long    ' number of 8k loops Dim LeftOver As Integer ' amount of file left Dim i As Integer        ' counter for loops Const MaxSize = 8192    ' maximum buffer size lReturn = OpenPrinter(Printer.DeviceName, lhPrinter, 0) If lReturn = 0 Then MsgBox &quot;The Printer Name you typed wasn't recognized.&quot; Exit Sub End If MyDocInfo.pDocName = &quot;My Document&quot; MyDocInfo.pOutputFile = vbNullString MyDocInfo.pDatatype = vbNullString lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo) ' Make sure that FILE_NAME points to a valid PCL or PostScript file on your system. Open FILE_NAME For Binary As #1 ' Calculate size of file and amount left over. NumLoops = LOF(1) \ MaxSize LeftOver = LOF(1) Mod MaxSize For i = 1 To NumLoops sWrittenData = Input(MaxSize, #1) lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _                 Len(sWrittenData), lpcWritten) Next ' Grab what is left over. If LeftOver <> 0 Then sWrittenData = Input(LeftOver, #1) lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _                     Len(sWrittenData), lpcWritten) End If Close #1 lReturn = EndDocPrinter(lhPrinter) lReturn = ClosePrinter(lhPrinter) End Sub

Private Sub Form_Load Command1.Caption = &quot;Print a String&quot; Command2.Caption = &quot;Print a File&quot; End Sub  Assign the constant FILE_NAME in the code with your own PCL or PostScript path and file name. Run the project. Click Print a String, which prints a string to the printer. Click Print a File to read and print a PCL or PostScript file directly to the printer.

