Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const PRINTER_ACCESS_ADMINISTER = &H4
Public Const PRINTER_ACCESS_USE = &H8
Public Const PRINTER_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED _
Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE
'Printer Control constants to use with SetPrinter
Public Const PRINTER_CONTROL_PAUSE = 1
Public Const PRINTER_CONTROL_RESUME = 2
Public Const PRINTER_CONTROL_PURGE = 3
'Null pointer
Public Const NULLPTR = 0&
Public Type PRINTER_INFO_2
pServerName As Long
pPrinterName As Long
pShareName As Long
pPortName As Long
pDriverName As Long
pComment As Long
pLocation As Long
pDevmode As Long ' Pointer to DEVMODE
pSepFile As Long
pPrintProcessor As Long
pDatatype As Long
pParameters As Long
pSecurityDescriptor As Long
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 Type PRINTER_DEFAULTS
pDatatype As Long
pDevmode As Long
DesiredAccess As Long
End Type
Public Declare Function OpenPrinter1 Lib "winspool.drv" _
Alias "OpenPrinterA" _
(ByVal pPrinterName As String, _
phPrinter As Long, _
pDefault As PRINTER_DEFAULTS) As Long
Public Declare Function SetPrinter Lib "winspool.drv" _
Alias "SetPrinterA" _
(ByVal hPrinter As Long, _
ByVal Level As Long, _
pPrinter As Any, _
ByVal Command As Long) As Long
Public Declare Function ClosePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long
Public Declare Function GetLastError Lib "kernel32.dll" () As Long
'Printer control enums
Public Enum PDXPrinterControl
pdxPrinterControlPause = PRINTER_CONTROL_PAUSE
pdxPrinterControlPurge = PRINTER_CONTROL_PURGE
pdxPrinterControlResume = PRINTER_CONTROL_RESUME
End Enum
'_____________________________________
Public Function fSetPrinterStatus(ByVal strPrinterName As String, _
ByVal lngStatus As PDXPrinterControl) As Boolean
'--- Sets the status of the specified printer
' to a PDXPrinterControl enum
'--- Parameters
' [In]
' strPrinterName: name of the printer to change
' lngStatus: status to set. One of pause, purge or resume
'--- Return value
' returns True if the status was set successfully else False if an error occurred
Dim pudtPD As PRINTER_DEFAULTS 'PRINTER_DEFAULTS structure
Dim pudtPI2 As PRINTER_INFO_2 'PRINTER_INFO_2 structure
Dim phPrinter As Long 'handle to printer
Dim plngSizeReq As Long 'Size in bytes needed to hold a structure
Dim plngRtn As Long 'Return value
Dim plngTmp As Long 'Temp buffer
Dim pblnRtn As Boolean 'return value
Dim pabytPI() As Byte 'Byte array to hold the PRINTER_DEFAULTS structure
'Get a handle to the printer.
pudtPD.DesiredAccess = PRINTER_ALL_ACCESS
plngRtn = OpenPrinter1(strPrinterName, phPrinter, pudtPD)
If plngRtn = 0 Or phPrinter = 0 Then
'Failed to open the printer
fSetPrinterStatus = False
Exit Function
End If
'Set the printer status
plngRtn = SetPrinter(phPrinter, NULLPTR, ByVal NULLPTR, lngStatus)
If plngRtn = 0 Then
'Status set failed
fSetPrinterStatus = False
GoTo ExitFunc
End If
'Return value
fSetPrinterStatus = True
ExitFunc:
If Not phPrinter = 0 Then
ClosePrinter phPrinter
End If
End Function