Results 1 to 5 of 5

Thread: Access the Printer Queue

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    276

    Access the Printer Queue

    Hi,
    I am getting to print PDF Files using the PDF.OCX component. The scenario is that at a time atleast 4 PDF files can be sent for Print. I need to trace the Printer Queue n figure out if all the files that were spooled have been successfully printed. If so, i should update a Flag in the database ... if not i need to prompt to the user.
    I dunno how to trace the Printer Queue. Any suggestions ?
    Kindly help.
    - Jemima.

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well I'm not expert on printing and what not, but you could try doing something like this ;

    Enumerate through the list of printers (or just the one or whatever), and the check the .Page property.
    If the pdf files all have more than one page, then you'd know once its finished printing because the .Page property for the printer should reset to 1.

    So in a do loop, with a doevents thrown in, you would check for when .Page is set to 1, and you know a print has been completed.

    There's probably a better way of doing it though.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Not my code but it works with local (I know) and network (I haven't tested) printers. Usage at end.

    HTH
    John

    '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 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 = CloseHandle(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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    276
    Thanks Pat,
    Will try it the first thing i reach my desk tomorrow morn.
    - Jemima.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    276
    thats a whole lot of useful code Pat....it worked! thanx.
    - Jemima.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width