'用GetPrinterStatus() Option Explicit Public Type DOCINFO cbSize As Long lpszDocName As String lpszOutput As String End Type Public 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 TypePublic 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 TypePublic Const JOB_STATUS_PRINTED = &H80 Public Const JOB_STATUS_PRINTING = &H10 Public Const JOB_STATUS_SPOOLING = &H8 Public Const JOB_STATUS_PAUSED = &H1Public Declare Function StartDoc Lib "gdi32" Alias "StartDocA" (ByVal hdc As Long, lpdi As DOCINFO) As Long Public Declare Function EndDoc Lib "gdi32" (ByVal hdc As Long) As LongPublic Declare Function EnumForms Lib "winspool.drv" Alias "EnumFormsA" _ (ByVal hPrinter As Long, ByVal Level As Long, ByRef pForm As Any, _ ByVal cbBuf As Long, ByRef pcbNeeded As Long, _ ByRef pcReturned As Long) As LongPublic Declare Function AddForm Lib "winspool.drv" Alias "AddFormA" _ (ByVal hPrinter As Long, ByVal Level As Long, pForm As Byte) As LongPublic Declare Function DeleteForm Lib "winspool.drv" Alias "DeleteFormA" _ (ByVal hPrinter As Long, ByVal pFormName As String) As Long
Public Declare Function OpenPrinter Lib "winspool.drv" _ Alias "OpenPrinterA" (ByVal pPrinterName As String, _ phPrinter As Long, ByVal pDefault As Long) As Long Public Declare Function MyOpenPrinter Lib "winspool.drv" Alias _ "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As _ Long, pDefault As Any) As Long Public Declare Function ClosePrinter Lib "winspool.drv" _ (ByVal hPrinter As Long) As Long Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Any, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As LongPublic Declare Function DocumentProperties Lib "winspool.drv" _ Alias "DocumentPropertiesA" (ByVal hwnd As Long, _ ByVal hPrinter As Long, ByVal pDeviceName As String, _ pDevModeOutput As Any, pDevModeInput As Any, ByVal fMode As Long) _ As LongPublic Declare Function ResetDC Lib "gdi32" Alias "ResetDCA" _ (ByVal hdc As Long, lpInitData As Any) As LongPublic Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _ (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)Public Declare Function lstrcpy Lib "KERNEL32" Alias "lstrcpyA" _ (ByVal lpString1 As String, ByRef lpString2 As Long) As Long
' Optional functions not used in this sample, but may be useful. Public Declare Function GetForm Lib "winspool.drv" Alias "GetFormA" _ (ByVal hPrinter As Long, ByVal pFormName As String, _ ByVal Level As Long, pForm As Byte, ByVal cbBuf As Long, _ pcbNeeded As Long) As Long
Public Declare Function SetForm Lib "winspool.drv" Alias "SetFormA" _ (ByVal hPrinter As Long, ByVal pFormName As String, _ ByVal Level As Long, pForm As Byte) As Long' Constants for DEVMODE Public Const CCHFORMNAME = 32 Public Const CCHDEVICENAME = 32 Public Const DM_FORMNAME As Long = &H10000 Public Const DM_ORIENTATION = &H1&' Constants for PRINTER_DEFAULTS.DesiredAccess Public Const PRINTER_ACCESS_ADMINISTER = &H4 Public Const PRINTER_ACCESS_USE = &H8 Public Const STANDARD_RIGHTS_REQUIRED = &HF0000 Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _ PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
' Constants for DocumentProperties() call Public Const DM_MODIFY = 8 Public Const DM_IN_BUFFER = DM_MODIFY Public Const DM_COPY = 2 Public Const DM_OUT_BUFFER = DM_COPY' Custom constants for this sample's SelectForm function Public Const FORM_NOT_SELECTED = 0 Public Const FORM_SELECTED = 1 Public Const FORM_ADDED = 2Public Type RECTL Left As Long Top As Long Right As Long Bottom As Long End TypePublic Type SIZEL cx As Long cy As Long End TypePublic Type SECURITY_DESCRIPTOR Revision As Byte Sbz1 As Byte Control As Long Owner As Long Group As Long Sacl As Long ' ACL Dacl As Long ' ACL End Type' The two definitions for FORM_INFO_1 make the coding easier. Public Type FORM_INFO_1 Flags As Long pName As Long ' String Size As SIZEL ImageableArea As RECTL End TypePublic Type sFORM_INFO_1 Flags As Long pName As String Size As SIZEL ImageableArea As RECTL End TypePublic 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 TypePublic Type PRINTER_DEFAULTS pDatatype As String pDevMode As Long ' DEVMODE DesiredAccess As Long End TypePublic Type PRINTER_INFO_2 pServerName As String pPrinterName As String pShareName As String pPortName As String pDriverName As String pComment As String pLocation As String pDevMode As DEVMODE pSepFile As String pPrintProcessor As String pDatatype As String pParameters As String pSecurityDescriptor As SECURITY_DESCRIPTOR Attributes As Long Priority As Long DefaultPriority As Long StartTime As Long UntilTime As Long Status As Long cJobs As Long AveragePPM As Long End Type
Public Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As Long, pcbNeeded As Long) As Long Public Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, ByVal pPrinter As Long, ByVal Command As Long) As Long Public Const PRINTER_CONTROL_PAUSE = 1 Public Const PRINTER_CONTROL_PURGE = 3 Public Const PRINTER_CONTROL_RESUME = 2 Public Const PRINTER_CONTROL_SET_STATUS = 4 Public Function GetPrinterStatus() As String Dim Job() As JOB_INFO_1 ReDim Job(10) Dim PrinterHandle As Long Dim ctl As Long Dim PrinterName As String Dim nbytes As Long, numEntries As Long Dim buf As String * 80 Dim i As Integer Dim pd As PRINTER_DEFAULTS pd.DesiredAccess = PRINTER_ALL_ACCESS PrinterName = Printer.DeviceName nbytes = 0 numEntries = 0 If MyOpenPrinter(PrinterName, PrinterHandle, pd) Then EnumJobs PrinterHandle, 0, 10, PRINTER_ALL_ACCESS, Job(0), Len(Job(0)), nbytes, numEntries For i = 0 To numEntries If Job(i).pStatus <> Null Then buf = Job(i).pStatus Else Select Case Job(i).Status Case JOB_STATUS_PAUSED: buf = " Paused" Case JOB_STATUS_PRINTED: buf = " Printed" Case JOB_STATUS_PRINTING: buf = " Printing" Case JOB_STATUS_SPOOLING: buf = " Spooling" End Select End If Next ClosePrinter PrinterHandle End If MsgBox buf End Function
Option Explicit
Public Type DOCINFO
cbSize As Long
lpszDocName As String
lpszOutput As String
End Type
Public 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 TypePublic 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 TypePublic Const JOB_STATUS_PRINTED = &H80
Public Const JOB_STATUS_PRINTING = &H10
Public Const JOB_STATUS_SPOOLING = &H8
Public Const JOB_STATUS_PAUSED = &H1Public Declare Function StartDoc Lib "gdi32" Alias "StartDocA" (ByVal hdc As Long, lpdi As DOCINFO) As Long
Public Declare Function EndDoc Lib "gdi32" (ByVal hdc As Long) As LongPublic Declare Function EnumForms Lib "winspool.drv" Alias "EnumFormsA" _
(ByVal hPrinter As Long, ByVal Level As Long, ByRef pForm As Any, _
ByVal cbBuf As Long, ByRef pcbNeeded As Long, _
ByRef pcReturned As Long) As LongPublic Declare Function AddForm Lib "winspool.drv" Alias "AddFormA" _
(ByVal hPrinter As Long, ByVal Level As Long, pForm As Byte) As LongPublic Declare Function DeleteForm Lib "winspool.drv" Alias "DeleteFormA" _
(ByVal hPrinter As Long, ByVal pFormName As String) As Long
Public Declare Function OpenPrinter Lib "winspool.drv" _
Alias "OpenPrinterA" (ByVal pPrinterName As String, _
phPrinter As Long, ByVal pDefault As Long) As Long
Public Declare Function MyOpenPrinter Lib "winspool.drv" Alias _
"OpenPrinterA" (ByVal pPrinterName As String, phPrinter As _
Long, pDefault As Any) As Long
Public Declare Function ClosePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long
Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Any, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As LongPublic Declare Function DocumentProperties Lib "winspool.drv" _
Alias "DocumentPropertiesA" (ByVal hwnd As Long, _
ByVal hPrinter As Long, ByVal pDeviceName As String, _
pDevModeOutput As Any, pDevModeInput As Any, ByVal fMode As Long) _
As LongPublic Declare Function ResetDC Lib "gdi32" Alias "ResetDCA" _
(ByVal hdc As Long, lpInitData As Any) As LongPublic Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _
(hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)Public Declare Function lstrcpy Lib "KERNEL32" Alias "lstrcpyA" _
(ByVal lpString1 As String, ByRef lpString2 As Long) As Long
' Optional functions not used in this sample, but may be useful.
Public Declare Function GetForm Lib "winspool.drv" Alias "GetFormA" _
(ByVal hPrinter As Long, ByVal pFormName As String, _
ByVal Level As Long, pForm As Byte, ByVal cbBuf As Long, _
pcbNeeded As Long) As Long
Public Declare Function SetForm Lib "winspool.drv" Alias "SetFormA" _
(ByVal hPrinter As Long, ByVal pFormName As String, _
ByVal Level As Long, pForm As Byte) As Long' Constants for DEVMODE
Public Const CCHFORMNAME = 32
Public Const CCHDEVICENAME = 32
Public Const DM_FORMNAME As Long = &H10000
Public Const DM_ORIENTATION = &H1&' Constants for PRINTER_DEFAULTS.DesiredAccess
Public Const PRINTER_ACCESS_ADMINISTER = &H4
Public Const PRINTER_ACCESS_USE = &H8
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
' Constants for DocumentProperties() call
Public Const DM_MODIFY = 8
Public Const DM_IN_BUFFER = DM_MODIFY
Public Const DM_COPY = 2
Public Const DM_OUT_BUFFER = DM_COPY' Custom constants for this sample's SelectForm function
Public Const FORM_NOT_SELECTED = 0
Public Const FORM_SELECTED = 1
Public Const FORM_ADDED = 2Public Type RECTL
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePublic Type SIZEL
cx As Long
cy As Long
End TypePublic Type SECURITY_DESCRIPTOR
Revision As Byte
Sbz1 As Byte
Control As Long
Owner As Long
Group As Long
Sacl As Long ' ACL
Dacl As Long ' ACL
End Type' The two definitions for FORM_INFO_1 make the coding easier.
Public Type FORM_INFO_1
Flags As Long
pName As Long ' String
Size As SIZEL
ImageableArea As RECTL
End TypePublic Type sFORM_INFO_1
Flags As Long
pName As String
Size As SIZEL
ImageableArea As RECTL
End TypePublic 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 TypePublic Type PRINTER_DEFAULTS
pDatatype As String
pDevMode As Long ' DEVMODE
DesiredAccess As Long
End TypePublic Type PRINTER_INFO_2
pServerName As String
pPrinterName As String
pShareName As String
pPortName As String
pDriverName As String
pComment As String
pLocation As String
pDevMode As DEVMODE
pSepFile As String
pPrintProcessor As String
pDatatype As String
pParameters As String
pSecurityDescriptor As SECURITY_DESCRIPTOR
Attributes As Long
Priority As Long
DefaultPriority As Long
StartTime As Long
UntilTime As Long
Status As Long
cJobs As Long
AveragePPM As Long
End Type
Public Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, ByVal pPrinter As Long, ByVal Command As Long) As Long
Public Const PRINTER_CONTROL_PAUSE = 1
Public Const PRINTER_CONTROL_PURGE = 3
Public Const PRINTER_CONTROL_RESUME = 2
Public Const PRINTER_CONTROL_SET_STATUS = 4
Public Function GetPrinterStatus() As String
Dim Job() As JOB_INFO_1
ReDim Job(10)
Dim PrinterHandle As Long
Dim ctl As Long
Dim PrinterName As String
Dim nbytes As Long, numEntries As Long
Dim buf As String * 80
Dim i As Integer
Dim pd As PRINTER_DEFAULTS
pd.DesiredAccess = PRINTER_ALL_ACCESS
PrinterName = Printer.DeviceName
nbytes = 0
numEntries = 0
If MyOpenPrinter(PrinterName, PrinterHandle, pd) Then
EnumJobs PrinterHandle, 0, 10, PRINTER_ALL_ACCESS, Job(0), Len(Job(0)), nbytes, numEntries
For i = 0 To numEntries
If Job(i).pStatus <> Null Then
buf = Job(i).pStatus
Else
Select Case Job(i).Status
Case JOB_STATUS_PAUSED:
buf = " Paused"
Case JOB_STATUS_PRINTED:
buf = " Printed"
Case JOB_STATUS_PRINTING:
buf = " Printing"
Case JOB_STATUS_SPOOLING:
buf = " Spooling"
End Select
End If
Next
ClosePrinter PrinterHandle
End If
MsgBox buf
End Function