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

Article translations Article translations
Article ID: 298141 - View products that this article applies to.
This article was previously published under Q298141
Expand all | Collapse all

On This Page

SUMMARY

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

MORE INFORMATION

Step-by-Step Example

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Place two Command Buttons, Command1 and Command2, onto Form1.
  3. 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 = "C:\TEST.PRN"  
    
    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
    
    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 "The Printer " & Printer.DeviceName & " could not be opened!"
          Exit Sub
      End If
      MyDocInfo.pDocName = "My Document"
      MyDocInfo.pOutputFile = vbNullString
      MyDocInfo.pDatatype = vbNullString
      lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
      Call StartPagePrinter(lhPrinter)
      sWrittenData = "How's that for Magic !!!!" & 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 "The Printer Name you typed wasn't recognized."
          Exit Sub
      End If
      MyDocInfo.pDocName = "My Document"
      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 = "Print a String"
      Command2.Caption = "Print a File"
    End Sub
    					
  4. Assign the constant FILE_NAME in the code with your own PCL or PostScript path and file name.
  5. 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.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
154078 How To Send Raw Data to a Printer Using the Win32 API from Visual Basic

Properties

Article ID: 298141 - Last Review: July 13, 2004 - Revision: 1.1
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
Keywords: 
kbhowto kbprint kbapi kbspooler kbcodesnippet kbsample KB298141

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com