VB and VBA Users Source Code: Get the number of print jobs in a print queue
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Get the number of print jobs in a print queue
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, April 10, 2001
Hits:
1266
Category:
Visual Basic General
Article:
The following routine can be used to return the number of print jobs waiting in a queue (for both local and network printers). Option Explicit 'Constants Definition Private Const CCHDEVICENAME = 32 Private Const CCHFORMNAME = 32 Private Const PRINTER_ACCESS_ADMINISTER = &H4 Private Const PRINTER_ACCESS_USE = &H8 'Types Definition Private Type DEVMODE dmDeviceName As String * CCHDEVICENAME dmSpecVersion As Integer dmDriverVersion As Integer dmSize As Integer dmDriverExtra As Integer dmFields As Long dmOrientation As Integer dmPaperSize As Integer dmPaperLength As Integer dmPaperWidth As Integer dmScale As Integer dmCopies As Integer dmDefaultSource As Integer dmPrintQuality As Integer dmColor As Integer dmDuplex As Integer dmYResolution As Integer dmTTOption As Integer dmCollate As Integer dmFormName As String * CCHFORMNAME dmUnusedPadding As Integer dmBitsPerPel As Long dmPelsWidth As Long dmPelsHeight As Long dmDisplayFlags As Long dmDisplayFrequency As Long End Type Private Type PRINTER_DEFAULTS pDatatype As String pDevMode As DEVMODE DesiredAccess As Long End Type Private Type SYSTEMTIME wYear As Integer wMonth As Integer wDayOfWeek As Integer wDay As Integer wHour As Integer wMinute As Integer wSecond As Integer wMilliseconds As Integer End Type Private Type JOB_INFO_1_API JobId As Long pPrinterName As Long pMachineName As Long pUserName As Long pDocument As Long pDatatype As Long pStatus As Long Status As Long Priority As Long Position As Long TotalPages As Long PagesPrinted As Long Submitted As SYSTEMTIME End Type Private Type JOB_INFO_1 JobId As Long pPrinterName As String pMachineName As String pUserName As String pDocument As String pDatatype As String pStatus As String Status As Long Priority As Long Position As Long TotalPages As Long PagesPrinted As Long Submitted As SYSTEMTIME End Type 'API Declarations Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long Private Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal HPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, ByVal pJob As Long, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long Private Declare Sub CopyMem Lib "kernel32.dll" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long) Private Declare Function lstrlenW Lib "kernel32.dll" (ByVal lpString As Long) As Long Private Declare Function HeapAlloc Lib "kernel32.dll" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long Private Declare Function GetProcessHeap Lib "kernel32.dll" () As Long Private Declare Function HeapFree Lib "kernel32.dll" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long 'Private structure filled by PrinterQueueItems with all the documents data in the selected printer queue Private JobsDesc(0 To 127) As JOB_INFO_1 'Purpose : Returns the number of atJobs in the specified Printer Queue 'Inputs : sPrinterName The name of the printer, ' can be a network path eg. "\\MYSERVER\MYPRINTER" 'Outputs : Returns the number of printer atJobs in queue 'Author : Andrew Baker 'Date : 25/11/2000 03:33 'Notes : Source code from Andrea Tincani. See http://www.andreavb.f2s.com 'Revisions : Function PrinterQueueItems(sPrinterName As String) As Long Dim tPrinterStruct As PRINTER_DEFAULTS Dim lhwndPrinter As Long Dim bRet As Boolean Dim atJobs(0 To 127) As JOB_INFO_1_API Dim lpcbNeeded As Long Dim lpcReturned As Long Dim lThisJob As Integer Dim lTempBuff As Long 'Initialize the Printer structure tPrinterStruct.pDatatype = vbNullString tPrinterStruct.pDevMode.dmSize = Len(tPrinterStruct.pDevMode) tPrinterStruct.DesiredAccess = PRINTER_ACCESS_USE 'Get the printer Handle bRet = OpenPrinter(sPrinterName, lhwndPrinter, tPrinterStruct) 'Get the Printer active atJobs bRet = EnumJobs(lhwndPrinter, 0, 127, 1, lTempBuff, 0, lpcbNeeded, lpcReturned) If lpcbNeeded = 0 Then PrinterQueueItems = 0 Else 'Allocate the Buffer lTempBuff = HeapAlloc(GetProcessHeap(), 0, lpcbNeeded) bRet = EnumJobs(lhwndPrinter, 0, 127, 1, lTempBuff, lpcbNeeded, lpcbNeeded, lpcReturned) CopyMem atJobs(0), ByVal lTempBuff, lpcbNeeded For lThisJob = 0 To lpcReturned - 1 JobsDesc(lThisJob).pPrinterName = LPSTRtoSTRING(atJobs(lThisJob).pPrinterName) JobsDesc(lThisJob).pMachineName = LPSTRtoSTRING(atJobs(lThisJob).pMachineName) JobsDesc(lThisJob).pUserName = LPSTRtoSTRING(atJobs(lThisJob).pUserName) JobsDesc(lThisJob).pDocument = LPSTRtoSTRING(atJobs(lThisJob).pDocument) JobsDesc(lThisJob).pDatatype = LPSTRtoSTRING(atJobs(lThisJob).pDatatype) JobsDesc(lThisJob).pStatus = LPSTRtoSTRING(atJobs(lThisJob).pStatus) JobsDesc(lThisJob).JobId = atJobs(lThisJob).JobId JobsDesc(lThisJob).Status = atJobs(lThisJob).Status JobsDesc(lThisJob).Priority = atJobs(lThisJob).Priority JobsDesc(lThisJob).Position = atJobs(lThisJob).Position JobsDesc(lThisJob).TotalPages = atJobs(lThisJob).TotalPages JobsDesc(lThisJob).PagesPrinted = atJobs(lThisJob).PagesPrinted JobsDesc(lThisJob).Submitted = atJobs(lThisJob).Submitted Next If lTempBuff Then HeapFree GetProcessHeap(), 0, lTempBuff PrinterQueueItems = lpcReturned End If 'Close printer bRet = ClosePrinter(lhwndPrinter) End Function 'Removes Null Characters Private Function TrimStr(strName As String) As String 'Finds a null then trims the string Dim x As Integer x = InStr(strName, vbNullChar) If x > 0 Then TrimStr = Left(strName, x - 1) Else TrimStr = strName End If End Function 'Returns a string from a pointer Private Function LPSTRtoSTRING(ByVal lngPointer As Long) As String Dim lngLength As Long 'Get number of characters in string lngLength = lstrlenW(lngPointer) * 2 'Initialize string so we have something to copy the string into LPSTRtoSTRING = String(lngLength, 0) 'Copy the string CopyMem ByVal StrPtr(LPSTRtoSTRING), ByVal lngPointer, lngLength 'Convert to Unicode LPSTRtoSTRING = TrimStr(StrConv(LPSTRtoSTRING, vbUnicode)) End Function 'Demonstration routine Sub Test() 'Get the Default Printer's Queue Debug.Print "Number of Documents in Queue: " & PrinterQueueItems(Printer.DeviceName) End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder