通过检查【Win32_Printer】的 Attributes 属性 的(二进制 1024) 位的状态【PRINTER_ATTRIBUTE_WORK_OFFLINE】: 1为脱机,0为联机
Attributes属性 的各个二进制位,代表了打印机各种状态,可通过公式提取出来:
如:提取其1024位的状态: (Int(objx.Attributes / 1024)) Mod 2
‘========VBA代码================
Private Const 分隔符 As String = ",", 引号 As String = "'"
Sub cs001()
MsgBox RPrinList
End Sub
Public Function RPrinList(Optional ByRef 默认打印机) As String '返回所有联机的打印机
Dim Str As String
If Printers.Count < 1 Then Exit Function '没有打印机
Str = ""
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colPrinters = objWMI.ExecQuery("Select * from Win32_Printer")
For Each objx In colPrinters
If (Int(objx.Attributes / 1024)) Mod 2 = 0 Then ' 检查是否脱机: PRINTER_ATTRIBUTE_WORK_OFFLINE:
Str = Str & 分隔符 & 引号 & objx.name & 引号
End If
Next
If Str <> "" Then Str = Mid(Str, 2)
RPrinList = Str
If Printers.Count = 1 Then 默认打印机 = Str ’只有一台打印机,返回该打印机名作为 默认打印机
End Function
'==============Attributes 属性 说明=======================
AttributesData type: uint32Access type: Read-only
Bitmap of attributes for a Windows-based printing device.
Value used to set the bit
|
Meaning
|
PRINTER_ATTRIBUTE_QUEUED1 (0x1)
|
Queued
Print jobs are buffered and queued.
|
PRINTER_ATTRIBUTE_DIRECT2 (0x2)
|
Direct
Document to be sent directly to the printer. This value is used if print jobs are not queued correctly.
|
PRINTER_ATTRIBUTE_DEFAULT4 (0x4)
|
Default
Default printer on a computer.
|
PRINTER_ATTRIBUTE_SHARED8 (0x8)
|
Shared
Available as a shared network resource.
|
PRINTER_ATTRIBUTE_NETWORK16 (0x10)
|
Network
Attached to a network. If both Local and Network bits are set, this indicates a network printer.
|
PRINTER_ATTRIBUTE_HIDDEN32 (0x20)
|
Hidden
Hidden from some users on the network.
|
PRINTER_ATTRIBUTE_LOCAL64 (0x40)
|
Local
Directly connected to a computer.
If both Local and Network bits are set, this indicates a network printer.
|
PRINTER_ATTRIBUTE_ENABLEDEVQ128 (0x80)
|
EnableDevQ
Enable the queue on the printer if available.
|
PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS256 (0x100)
|
KeepPrintedJobs
Spooler should not delete documents after they are printed.
|
PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST512 (0x200)
|
DoCompleteFirst
Start jobs that are finished spooling first.
|
PRINTER_ATTRIBUTE_WORK_OFFLINE1024 (0x400)
|
WorkOffline
Queue print jobs when a printer is not available.
|
PRINTER_ATTRIBUTE_ENABLE_BIDI2048 (0x800)
|
EnableBIDI
Enable bidirectional printing.
|
PRINTER_ATTRIBUTE_RAW_ONLY4096 (0x1000)
|
Allow only raw data type jobs to be spooled.
|
PRINTER_ATTRIBUTE_PUBLISHED8192 (0x2000)
|
Published
Published in the network directory service.
|