We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2
Public Function IsPrinterReady(Optional strPrinter As String = "") As Boolean
'Returns True if printer is turned on and ready.
'If no strPrintername provided, active printer is assumed. 'VBA code by Mor Sagmon
If (strPrinter = "") Then
strPrinter = Application.Printer IsPrinterReady = False End If
'If strPrinter has port part - discard
Dim intOnPosition As Integer
intOnPosition = InStr(1, strPrinter, " on ")
If (intOnPosition > 0) Then strPrinter = left(strPrinter, intOnPosition - 1) Else intOnPosition = InStr(1, strPrinter, " " & Chr(225)) 'Replace 225 with ASCII code as per your own Windows language. If (intOnPosition > 0) Then strPrinter = left(strPrinter, intOnPosition - 1) End If End If
On Error GoTo FailedPrinter
IsPrinterReady = Not GetObject("winmgmts:\\.\root\CIMV2").Get("Win32_Printer='"
& strPrinter & "'").WorkOffline
Exit Function
FailedPrinter:
'Implement error handling here if necessary
End Function
Public Function PrinterOffline(Optional pstrPrinter As String = "Default") As
Boolean 'Printer status 'Other (1) 'Unknown (2) 'Idle (3) 'Printing (4) 'Warmup (5) 'Stopped Printing(6) 'Offline (7) Dim strWhere As String Dim objWMI As Object Dim objPrinters As Object Dim objPrinter As Object
Set objWMI = GetObject("winmgmts:\\.\root\CIMV2")
If LCase$(pstrPrinter) = "default" Then strWhere = "Default = True" Else strWhere = "Name = '" & pstrPrinter & "'" End If Set objPrinters = objWMI.ExecQuery("SELECT * FROM Win32_Printer WHERE " & strWhere) For Each objPrinter In objPrinters PrinterOffline = objPrinter.WorkOffline 'Write the results to the worksheet. ' MsgBox objPrinter.Name & " " & objPrinter.PrinterStatus & " " & objPrinter.Local If (objPrinter.Default) Then getDefaultPrinter = objPrinter.Name End If Exit For Next
Set objPrinter = Nothing
Set objPrinters = Nothing Set objWMI = Nothing End Function