Atlanta Custom Software Development 

   Search        Code/Page

User Login



Forgot the Password?
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
» Regular Expr Tester
» Free Tools

The Visual Basic Printer object allows for printing through printer drivers, but there may be times when it is desirable to use the Win32 API to send information more directly to the printer. The code sample to follow shows how to achieve this by using API functions that bypass printer drivers to communicate directly with the print spooler.

For Quick Demo

1. Start a new project in Visual Basic. Form1 is created by default.
2. Place a Command Button on the form.
3. Add the following code to the Form1 code window:

Click here to copy the following block
Option Explicit

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 Declare Function AbortPrinter Lib "winspool.drv" (ByVal _
    hPrinter 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 Name you typed wasn't recognized."
    Exit Sub
  End If

  MyDocInfo.pDocName = "DemoPage"
  MyDocInfo.pOutputFile = vbNullString
  MyDocInfo.pDatatype = vbNullString

  lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
  Call StartPagePrinter(lhPrinter)
  sWrittenData = "Demo of Prining API" & vbFormFeed

  lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _
      Len(sWrittenData), lpcWritten)

  '///// Demo for Deleting spool file '////////
  If lReturn Then
    If MsgBox("Data written to the spool file." & vbCrLf & _
        "Do you want to cancel the job. " & _
        "If you press yes then it will delete the spool file." _
        , vbYesNo + vbQuestion) = vbYes Then
      Call AbortPrinter(lhPrinter)
      MsgBox "Job canceled and Spool file deleted", vbExclamation
    End If
  End If

  lReturn = EndPagePrinter(lhPrinter)
  lReturn = EndDocPrinter(lhPrinter)
  lReturn = ClosePrinter(lhPrinter)
End Sub

Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with, SQL Server and other MS technologies. He is, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )

Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.