Printer status opvragen

Status
Niet open voor verdere reacties.

PhilJ

Gebruiker
Lid geworden
22 sep 2020
Berichten
12
Hallo,

Ik ben op zoek naar vba code dat de printer status van de actieve printer weergeeft. Via google zijn er veel voorbeelden te vinden echter heb ik niets kunnen vinden wat werkt.
Kunnen jullie mij hier mee helpen?
 
Kun je aangeven wat je wil zien en waarvoor ?
 
Dit wil ik gebruiken in een ‘info’ formulier die zichtbaar is naast een facturatie blad. In dit info form wil ik graag de printernaam zien (heb ik) maar ook de aktuele status, denk hierbij aan idle, online, offline en printing ed.
 
Maar met die informatie kun je toch helemaal niets ?
 
Waardeloze info is ook info...:p

Toch zou ik het graag zien :cool:
 
Code:
Sub try()
Dim AllePrinterData As Object, printernaam as string
printernaam = "naam van jouw printer hier, met UNC pad en zonder On....."
Set AllePrinterData = GetObject("winmgmts:\\.\root\CIMV2").Get("Win32_Printer='" & printernaam & "'")
Stop
End Sub

Bekijk in jouw VBE editor onder Locals welke 'waardeloze' info je uit AllePrinterdata wilt halen
 
Er is geen betrouwbare manier om te zien als een printer aan of uit staat (met WMI).
Als de printer niet netjes zijn status terugmeldt aan het spoolsv.exe process, zal je altijd status 3=idle krijgen bij de eigenschap "printerstatus".

Note

If you are retrieving PrinterStatus = 3 or PrinterState = 0, the printer driver may not be feeding accurate information into WMI. WMI retrieves the printer information from the spoolsv.exe process. It is possible the printer driver does not report its status to the spooler. In this case, Win32_Printer reports the printer as Idle.
bron: https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printer?redirectedfrom=MSDN
 
Laatst bewerkt:
Code:
Sub M_snb()
  ReDim sn(27)

  For Each it In GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_PrintJob")
    sn(0) = "Caption" & vbTab & vbTab & it.Caption
    sn(1) = "Description" & vbTab & it.Description
    sn(2) = "InstallDate" & vbTab & it.InstallDate
    sn(3) = "Name" & vbTab & vbTab & it.Name
    sn(4) = "Status" & vbTab & vbTab & it.Status
    sn(5) = "ElapsedTime" & vbTab & it.ElapsedTime
    sn(6) = "JobStatus" & vbTab & it.JobStatus
    sn(7) = "Notify" & vbTab & vbTab & it.Notify
    sn(8) = "Owner" & vbTab & vbTab & it.Owner
    sn(9) = "Priority" & vbTab & vbTab & it.Priority
    sn(10) = "StartTime" & vbTab & it.StartTime
    sn(11) = "TimeSubmitted" & vbTab & it.TimeSubmitted
    sn(12) = "UntilTime" & vbTab & it.UntilTime
    sn(14) = "DataType" & vbTab & vbTab & it.DataType
    sn(15) = "Document" & vbTab & it.Document
    sn(16) = "DriverName" & vbTab & it.DriverName
    sn(17) = "HostPrintQueue" & vbTab & it.HostPrintQueue
    sn(18) = "JobId" & vbTab & vbTab & it.JobId
    sn(19) = "PagesPrinted" & vbTab & it.PagesPrinted
    sn(23) = "Parameters" & vbTab & it.Parameters
    sn(24) = "PrintProcessor" & vbTab & it.PrintProcessor
    sn(25) = "Size" & vbTab & vbTab & it.Size
    sn(26) = "StatusMask" & vbTab & it.StatusMask
    sn(27) = "TotalPages" & vbTab & it.TotalPages

    MsgBox Join(sn, vbLf)
  Next
End Sub
 
Er is geen betrouwbare manier om te zien als een printer aan of uit staat (met WMI).
Als de printer niet netjes zijn status terugmeldt aan het spoolsv.exe process, zal je altijd status 3=idle krijgen bij de eigenschap "printerstatus".

bron: https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printer?redirectedfrom=MSDN

Wat ik ook probeerde, ik liep idd tegen bovenstaand probleem aan. Ik ga er dan ook maar van uit dat het niet mogelijk is.
Iedereen bedankt voor de hulp.
 
Staat je printer in een netwerk met een ip-adress.
Zo ja dan kan je zoeken op "snmp"
Maar ik kan daar geen code voor schrijven omdat ik geen netwerk heb.
 
Nee, het gaat om een lokale printer

Laatste code die ik heb geprobeerd geeft ook een offline status:

Code:
Option Explicit

Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long
Public Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Byte, ByVal cbBuf As Long, pcbNeeded As Long) As Long
Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
   
Public Type PRINTER_DEFAULTS
   pDatatype As String
   pDevMode As Long
   DesiredAccess As Long
End Type
Public Const PRINTER_ACCESS_USE = &H8

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
   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 Const ERROR_INSUFFICIENT_BUFFER = 122
Public Const PRINTER_STATUS_BUSY = &H200
Public Const PRINTER_STATUS_DOOR_OPEN = &H400000
Public Const PRINTER_STATUS_ERROR = &H2
Public Const PRINTER_STATUS_INITIALIZING = &H8000
Public Const PRINTER_STATUS_IO_ACTIVE = &H100
Public Const PRINTER_STATUS_MANUAL_FEED = &H20
Public Const PRINTER_STATUS_NO_TONER = &H40000
Public Const PRINTER_STATUS_NOT_AVAILABLE = &H1000
Public Const PRINTER_STATUS_OFFLINE = &H80
Public Const PRINTER_STATUS_OUT_OF_MEMORY = &H200000
Public Const PRINTER_STATUS_OUTPUT_BIN_FULL = &H800
Public Const PRINTER_STATUS_PAGE_PUNT = &H80000
Public Const PRINTER_STATUS_PAPER_JAM = &H8
Public Const PRINTER_STATUS_PAPER_OUT = &H10
Public Const PRINTER_STATUS_PAPER_PROBLEM = &H40
Public Const PRINTER_STATUS_PAUSED = &H1
Public Const PRINTER_STATUS_PENDING_DELETION = &H4
Public Const PRINTER_STATUS_PRINTING = &H400
Public Const PRINTER_STATUS_PROCESSING = &H4000
Public Const PRINTER_STATUS_TONER_LOW = &H20000
Public Const PRINTER_STATUS_USER_INTERVENTION = &H100000
Public Const PRINTER_STATUS_WAITING = &H2000
Public Const PRINTER_STATUS_WARMING_UP = &H10000
' ------------------------------------------------------
Function PrinterName()

If PrinterName = "" Then
PrinterName = Application.ActivePrinter
End If

    Dim intOnPosition As Integer

    intOnPosition = InStr(1, PrinterName, " op ")

    If (intOnPosition > 0) Then
        PrinterName = Left(PrinterName, intOnPosition - 1)
    End If
        
End Function


Public Function CheckPrinter() As String
On Error GoTo Hell

    Dim hPrinter As Long
    Dim iByteBuffer As Long
    Dim iBytesNeeded As Long
    Dim tPI2 As PRINTER_INFO_2
    Dim aPrinterInfo() As Byte
    Dim Ret As Long
    Dim sPrinterName As String
    Dim tDefaults As PRINTER_DEFAULTS
    
    'Use the default printer of Printers collection.
    sPrinterName = PrinterName
    
    'Set desired access security setting.
    tDefaults.DesiredAccess = PRINTER_ACCESS_USE
    
    'Call API to get a handle to the printer.
    Ret = OpenPrinter(sPrinterName, hPrinter, tDefaults)
    If Ret = 0 Then
        CheckPrinter = "The system cannot open printer " & sPrinterName & ". Windows returned this error: " & Err.LastDllError
        Exit Function
    End If
    
    'Init iBytesNeeded
    iBytesNeeded = 0
    
    'Clear the error object of any errors.
    Err.Clear
    
    'Determine the buffer size that is needed to get printer info.
    Ret = GetPrinter(hPrinter, 2, 0&, 0&, iBytesNeeded)
'    If Err.LastDllError = ERROR_INSUFFICIENT_BUFFER Then
        'CheckPrinter = "The system failed to initialize the GetPrinter() API call due to insufficient buffer allocation."
    'Else
        ReDim aPrinterInfo(1 To iBytesNeeded)
        iByteBuffer = iBytesNeeded
        
        'Call GetPrinter to get the status.
        Ret = GetPrinter(hPrinter, 2, aPrinterInfo(1), iByteBuffer, iBytesNeeded)
        
        If Ret = 0 Then
            CheckPrinter = "The system could not determine the printer Status. Windows printing subsystem returned this error:" & Err.LastDllError
        Else
        
            'Copy contents of printer status byte array into a
            'PRINTER_INFO_2 structure to separate the individual elements.
            CopyMemory tPI2, aPrinterInfo(1), Len(tPI2)
            
            If (tPI2.Status And PRINTER_STATUS_BUSY) Then CheckPrinter = "The printer returned a status of: Busy"
            If (tPI2.Status And PRINTER_STATUS_DOOR_OPEN) Then CheckPrinter = "The printer returned a status of: Printer Door Open"
            If (tPI2.Status And PRINTER_STATUS_ERROR) Then CheckPrinter = "The printer returned a status of: Printer Error"
            If (tPI2.Status And PRINTER_STATUS_NO_TONER) Then CheckPrinter = "The printer returned a status of: No Toner"
            If (tPI2.Status And PRINTER_STATUS_NOT_AVAILABLE) Then CheckPrinter = "The printer returned a status of: Not Available"
            If (tPI2.Status And PRINTER_STATUS_OFFLINE) Then CheckPrinter = "The printer returned a status of: Off Line"
            If (tPI2.Status And PRINTER_STATUS_OUT_OF_MEMORY) Then CheckPrinter = "The printer returned a status of: Out of Memory"
            If (tPI2.Status And PRINTER_STATUS_PAGE_PUNT) Then CheckPrinter = "The printer returned a status of: Page Punt"
            If (tPI2.Status And PRINTER_STATUS_PAPER_JAM) Then CheckPrinter = "The printer returned a status of: Paper Jam"
            If (tPI2.Status And PRINTER_STATUS_PAPER_OUT) Then CheckPrinter = "The printer returned a status of: Paper Out"
            If (tPI2.Status And PRINTER_STATUS_PAPER_PROBLEM) Then CheckPrinter = "The printer returned a status of: Page Problem"
            If (tPI2.Status And PRINTER_STATUS_PAUSED) Then CheckPrinter = "The printer returned a status of: Paused"
        End If
    'End If
        
    'Close the printer handle.
    ClosePrinter hPrinter
    
Hell:
End Function
 
Laatst bewerkt:
@ Allemaal

Ik ben wat op het spoor het lijkt erop dat je de status kan aflezen met de eigenschap "workoffline".
Maar dit lijkt erop te werken bij mij
  • Windows10
  • printernaam EPSON Stylus SX600FW
  • printer driver Epson ESC/P-R V4 Class Driver
  • printer processor winprint/RAW
  • port USB001
Kunnen jullie testen (printer uit, waarde noteren, printer aan, waarde noteren) en mij iets laten weten (met een paar eigenschappen zoals hierboven), alvast bedankt.
edit:
Workoffline is blijkbaar een indirecte indicator, wanneer de printer uitstaat mag je toch opdrachten verzenden.
Deze opdrachten blijven in de wacht staan, tot de printer weer online is.

Code:
Option Explicit

Private Function PrinterWorkOffline(sPrinter) As Boolean
    PrinterWorkOffline = CreateObject("WbemScripting.SWbemLocator").ConnectServer(".", "root\cimv2").ExecQuery("SELECT * FROM Win32_Printer WHERE Name = '" & sPrinter & "'").itemindex(0).workoffline
End Function

Public Sub Main()
    Dim bWorkOffline As Boolean
    bWorkOffline = PrinterWorkOffline("EPSON Stylus SX600FW") 'printername
End Sub
 
Laatst bewerkt:
Win10
Naam: NPIA3B015 (HP Color LaserJet MFP M277dw)

Device SWD\PRINTENUM\{F87AF0E7-6608-4042-93B9-298CA6655197} was configured.

Driver Name: printqueue.inf
Class Guid: {1ed2bbf9-11f0-4084-b21f-ad83a8e6dcdc}
Driver Date: 06/21/2006
Driver Version: 10.0.18362.1
Driver Provider: Microsoft
Driver Section: NO_DRV_LOCAL
Driver Rank: 0x0
Matching Device Id: PRINTENUM\LocalPrintQueue
Outranked Drivers: c_swdevice.inf:SWD\GenericRaw:00FF3001
Device Updated: false
Parent Device: SWD\PRINTENUM\WSD-2c343e4b-4910-4f52-917a-ab57868ffe1e.003a

Poort: Zie bijlage

Sttatus veranderd niet als de printer aan of uit staat.
 

Bijlagen

  • Knipsel.PNG
    Knipsel.PNG
    27,1 KB · Weergaven: 33
Werkt misschien alleen maar met apparaten op een USB-poort.
 
Dat krijg je toch ook te zien met:

Code:
Sub M_snb()
    Application.Dialogs(8).Show
End Sub
 
@snb
Na het laten lopen van je code.
Printer aan of uit, de status blijft "Niet-actief".
Clipboard01.png
 
Laatst bewerkt:
Misschien omdat ik niet zo'n dure printer heb:

goedkoop.PNG
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan