Senin, 18 Oktober 2010

Printer API simple code

I have a program that prints up to ten books at a time on a printer specified by the user at setup. This is the printer that I'm trying to receive messages from. I've got that name stored in a database and read it into the SetActivePrinter function as follows:

'Set connection and recordset for the database.
Set cnBookPrinter = New ADODB.Connection
Set rsBookPrinter = New ADODB.Recordset

'Open connection to the database
cnBookPrinter.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & App.Path & "\Book_Printer_II.mdb"

'Open recordset and get the name of the currently selected Book Printer
rsBookPrinter.Open "Select PrinterName from tblPrinters Order By PrinterName", _
cnBookPrinter, adOpenKeyset, adLockOptimistic

'Set book printer name variable value.
strPrinterName = rsBookPrinter!PrinterName

'If the recordset is empty, then there is no printer defined and the
'form for defining a printer is loaded.
If rsBookPrinter.RecordCount = 0 Then
Timer1.Enabled = False
frmBookPrinter.Show
frmAddPrinter.Show
Else
'If there is a printer defined, then we find the name of the printer
'and set the access for the printer.
Set vbPrinter = New ApiPrinter
vbPrinter.DesiredAccess = PRINTER_ACCESS_USE
vbPrinter.DeviceName = strPrinterName
Set ApiLink = New EventVB.APIFunctions
Call SetActivePrinter(strPrinterName)
End If

This code seems to be working just fine....I also have a timer on my form that is supposed to check the printer for status changes and I think this is where I'm getting confused as to what I should be using. That code is:

Private Sub Timer1_Timer()
Dim strCurrentStatus As String
Set vbPrintJob = New ApiPrintJob
If vbPrinter.Status = PRINTER_STATUS_READY Then
strCurrentStatus = vbPrinter.DeviceName & " is ready to print and there are no print jobs."
ElseIf vbPrinter.Status = PRINTER_STATUS_USER_INTERVENTION Then
strCurrentStatus = strCurrentStatus & " Printer awaiting manual feed."
ElseIf vbPrinter.Status = PRINTER_STATUS_DOOR_OPEN Then
strCurrentStatus = strCurrentStatus & " Printer door is open."
ElseIf vbPrinter.Status = PRINTER_STATUS_ERROR Then
strCurrentStatus = strCurrentStatus & " Printer Error!"
ElseIf vbPrinter.Status = PRINTER_STATUS_INITIALIZING Then
strCurrentStatus = strCurrentStatus & " Printer is initializing..."
ElseIf vbPrinter.Status = PRINTER_STATUS_POWER_SAVE Then
strCurrentStatus = strCurrentStatus & " Printer in power save mode."
ElseIf vbPrinter.Status = PRINTER_STATUS_OFFLINE Then
strCurrentStatus = strCurrentStatus & " Printer is offline."
ElseIf vbPrinter.Status = PRINTER_STATUS_OUT_OF_MEMORY Then
strCurrentStatus = strCurrentStatus & " Printer out of memory."
ElseIf vbPrinter.Status = PRINTER_STATUS_PAPER_OUT Then
strCurrentStatus = strCurrentStatus & " Printer out of paper."
ElseIf vbPrinter.Status = PRINTER_STATUS_NO_TONER Then
strCurrentStatus = strCurrentStatus & " Printer is out of toner."
ElseIf vbPrinter.Status = PRINTER_STATUS_OUTPUT_BIN_FULL Then
strCurrentStatus = strCurrentStatus & " Printer output bin full."
ElseIf vbPrinter.Status = PRINTER_STATUS_PAPER_JAM Then
strCurrentStatus = strCurrentStatus & " Paper Jam!"
ElseIf vbPrinter.Status = PRINTER_STATUS_BUSY Or vbPrinter.Status = PRINTER_STATUS_PRINTING Then
strCurrentStatus = strCurrentStatus & " Printer is busy..."
End If

stbPrinterStatus.SimpleText = strCurrentStatus
End Sub

Tidak ada komentar:

Posting Komentar