Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long
OpenPrinter opens a printer by obtaining a handle to it. This handle can then be used by a number of printer API functions to reference the printer. After your program is finished using this handle, it should close the printer via ClosePrinter.
If an error occured, the function returns 0 (use GetLastError to get the error code). If successful, the function returns a non-zero value.
When passing 0 for pDefault, the expression ByVal CLng(0) must be used.
' Display a little information about each print job in the
' queue for the default printer. Note how both EnumPrinters and
' EnumJobs first copy the data into the arrays before placing
' the information into the desired structure.
Dim pi1 As PRINTER_INFO_1 ' holds a little information about the printer
Dim hPrinter As Long ' handle to the default printer once it is opened
Dim arraybuf() As Long ' resizable array used as a buffer
Dim jobinfo As JOB_INFO_2 ' holds detailed info about a print job
Dim needed As Long ' receives space needed in the buffer array
Dim numitems As Long ' receives the number of items returned
Dim lendivfour As Long ' the size in Long-type units of the jobinfo structure
Dim c As Long ' counter variable
Dim retval As Long ' return value
' -- Get the name of the default printer. --
' Determine how much space is needed to get the printer information.
retval = EnumPrinters(PRINTER_ENUM_DEFAULT, "", 1, ByVal 0, 0, needed, numitems)
' Resize the array buffer to the needed size in bytes.
ReDim arraybuf(0 To needed / 4 - 1) ' remember each element is 4 bytes
' Retrieve the information about the default printer.
retval = EnumPrinters(PRINTER_ENUM_DEFAULT, "", 1, arraybuf(0), needed, needed, numitems)
' Copy the printer name into the structure. The rest is unnecessary.
pi1.pName = Space(lstrlen(arraybuf(2)))
retval = lstrcpy(pi1.pName, arraybuf(2))
' -- Obtain a handle to the default printer (using default configuration). --
retval = OpenPrinter(pi1.pName, hPrinter, ByVal CLng(0))
' -- Enumerate the default printer's print jobs currently queued. --
' Determine how much space is needed to get the print jobs' information.
retval = EnumJobs(hPrinter, 0, 100, 2, ByVal 0, 0, needed, numitems)
' Resize the array buffer to the needed size in bytes.
ReDim arraybuf(0 To needed / 4 - 1) ' remember each element is 4 bytes
' Retrieve the information about the print jobs.
retval = EnumJobs(hPrinter, 0, 100, 2, arraybuf(0), needed, needed, numitems)
' Display the number of print jobs currently in the queue.
If numitems > 0 Then
Debug.Print "There are"; numitems; "print jobs currently in the queue."
Else
Debug.Print "No print jobs are currently in the queue."
End If
' For each print job, copy its data into the structure. Then display selected
' information from the structure. For brevity, this example copies only a
' few of the data members into the structure.
lendivfour = Len(jobinfo) / 4 ' this is the number of elements for each structure in the array
For c = 0 To numitems - 1 ' loop through each item
' Copy selected information into the structure: the job ID number, the
' name of the user who printed it, the total number of pages, and
' the time it was added into the queue.
jobinfo.JobID = arraybuf(lendivfour * c) ' the first element of the array chunk
jobinfo.pUserName = Space(lstrlen(arraybuf(lendivfour * c + 3))) ' fourth element
retval = lstrcpy(jobinfo.pUserName, arraybuf(lendivfour * c + 3))
jobinfo.TotalPages = arraybuf(lendivfour * c + 18) ' nineteenth element
CopyMemory jobinfo.Submitted, arraybuf(lendivfour * c + 20), Len(jobinfo.Submitted) ' twenty-first element
' Display the copied information.
Debug.Print "Job ID number:"; jobinfo.JobID
Debug.Print "Printed by user: "; jobinfo.pUserName
Debug.Print "Number of pages:"; jobinfo.TotalPages
Debug.Print "Placed in queue on: ";
' (display the date and time stored in jobinfo.Submitted)
Debug.Print jobinfo.Submitted.wMonth; "-"; jobinfo.Submitted.wDay; "-"; jobinfo.Submitted.wYear; " ";
Debug.Print jobinfo.Submitted.wHour; ":"; jobinfo.Submitted.wMinute; ":"; jobinfo.Submitted.wSecond; " GMT"
Next c
' Close the printer handle now that it is no longer needed.
retval = ClosePrinter(hPrinter)
Last Modified: November 28, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information