Click to See Complete Forum and Search --> : Visual Basic-Print Spooling


satheesh s
September 12th, 2001, 03:08 AM
We are using the following code for sending printing instructions to the Dot Matrix Printers, but we are not able to spool on to the printer on converting to executable.
Running .VBP through VB we able spool on to the printer.
Please suggest, what would be reason on why the executable is unable to spool.

******** VB CODE **********

Option Explicit

Private Type DOCINFO
cbSize As Long
lpszDocName As String
lpszOutput 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, 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 StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pDocInfo As Byte) 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 GetLastError Lib "kernel32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)

Private Declare Function StartDoc Lib "gdi32" Alias "StartDocA" (ByVal hDC As Long, lpdi As DOCINFO) As Long
Private Declare Function EndDoc Lib "gdi32" (ByVal hDC As Long) As Long

Public cnnPrintTest As New ADODB.Connection 'Connection Object

Private Sub Command1_Click()
Dim lhprinter As Long
Dim lReturn As Long
Dim lpcWritten As Long
Dim strData As String
Dim lDoc As Long
Dim MyDocInfo As DOCINFO
Dim lngI As Long

Dim recPrintTest As New ADODB.Recordset
Dim recGetDtl As New ADODB.Recordset

Dim strPrintName As String

lReturn = OpenPrinter("\\sys-70\EPSON FX-80+", lhprinter, 0)

If lReturn = 0 Then
MsgBox "Error, while opening printer", vbCritical
End
End If

MyDocInfo.cbSize = Len(MyDocInfo)
MyDocInfo.lpszDocName = "BILL Print"
MyDocInfo.lpszOutput = vbNullString

lDoc = StartDocPrinter(lhprinter, 1, 0)
If lDoc <= 0 Then
MsgBox "Cannot Spool to the file", vbCritical
End If
Call StartPagePrinter(lhprinter)

strData = "This is to be printed" & vbNewLine
'strData = vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & "-"
lReturn = WritePrinter(lhprinter, ByVal strData, Len(strData), 1)

strData = "This is to be printed" & vbNewLine
'strData = vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & "-"
lReturn = WritePrinter(lhprinter, ByVal strData, Len(strData), 1)
lReturn = EndDocPrinter(lhprinter)
lReturn = ClosePrinter(lhprinter)

End Sub