ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1524|回复: 1

[原创] 打印机 状态检测 【脱机】等

[复制链接]

TA的精华主题

TA的得分主题

发表于 2014-10-9 12:41 | 显示全部楼层 |阅读模式
本帖最后由 山中老人 于 2014-10-10 16:33 编辑

        通过检查【Win32_Printer】的 Attributes 属性 的(二进制 1024) 位的状态【PRINTER_ATTRIBUTE_WORK_OFFLINE】: 1为脱机,0为联机
        Attributes属性 的各个二进制位,代表了打印机各种状态,可通过公式提取出来:
        如:提取其1024位的状态: (Int(objx.Attributes / 1024)) Mod 2
        ‘========VBA代码================
  1. Private Const 分隔符 As String = ",", 引号 As String = "'"

  2. Sub cs001()
  3.     MsgBox RPrinList
  4. End Sub

  5. Public Function RPrinList(Optional ByRef 默认打印机) As String  '返回所有联机的打印机
  6.     Dim Str As String
  7.     If Printers.Count < 1 Then Exit Function '没有打印机
  8.     Str = ""
  9.     Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  10.     Set colPrinters = objWMI.ExecQuery("Select * from Win32_Printer")   
  11.     For Each objx In colPrinters
  12.         If (Int(objx.Attributes / 1024)) Mod 2 = 0 Then ' 检查是否脱机:  PRINTER_ATTRIBUTE_WORK_OFFLINE:
  13.             Str = Str & 分隔符 & 引号 & objx.name & 引号
  14.         End If
  15.     Next
  16.     If Str <> "" Then Str = Mid(Str, 2)
  17.     RPrinList = Str
  18.     If Printers.Count = 1 Then 默认打印机 = Str ’只有一台打印机,返回该打印机名作为 默认打印机
  19. End Function

  20. Public Function ScanPrinter(打印机 As String) As Boolean '检查指定打印机是否可用
  21.     If 打印机 = "" Then
  22.         MsgBox "未设置打印机!", , "错误"
  23.         Exit Function
  24.     End If
  25.     Dim Str As String
  26.     Str = RPrinList()
  27.     If InStr(Str, 引号 & 打印机 & 引号) < 1 Then
  28.         MsgBox "未找到打印机:" & 打印机, , "错误"
  29.         Exit Function
  30.     End If
  31.     ScanPrinter = True
  32. End Function
复制代码



       '==============Attributes 属性 说明=======================
        Attributes
Data type: uint32      Access 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.                                       

TA的精华主题

TA的得分主题

发表于 2014-10-10 06:43 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-6-10 12:59 , Processed in 0.028381 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表