Option Explicit
Dim aa$, bb$, starttm&, i%
Dim objWMIService, objProcess, colProcess, strComputer, strList, colInstalledPrinters, objPrinter
Private Sub Form_Load()
Timer1.Interval = 500
Timer1.Enabled = False
Command1.Caption = "打 印"
End Sub
Private Sub Command1_Click()
If Command1.Caption = "打 印" Then
If Printers.Count > 0 And Printer.DriverName <> "" Then
Command1.Caption = "取 消"
Open "c:\tt.txt" For Input As #1
starttm = Timer
Timer1.Enabled = True
Me.Caption = "打印机检测中"
While Not EOF(1)
Line Input #1, aa
Printer.Print aa
Wend
Close #1
Printer.EndDoc
Else
MsgBox "您未设置打印机或未安装驱动程序"
End If
Else
Command1.Caption = "打 印"
Timer1.Enabled = False
Printer.KillDoc
End If
End Sub
Private Sub Timer1_Timer()
strComputer = "."
Set objWMIService = GetObject("winmgmts:" + "{impersonationLevel=impersonate}!\\" + strComputer + "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("SELECT * FROM Win32_Printer")
bb = ""
For Each objPrinter In colInstalledPrinters
bb = bb & objPrinter.PrinterStatus
If InStr(bb, "4") > 0 Then Timer1.Enabled = False: Command1_Click: Exit Sub
Next
If Timer - starttm > 15 Then
Timer1.Enabled = False
MsgBox "打印机未联线或缺纸或予热中"
Command1_Click
End If
End Sub
相关代码:
Option Explicit
Declare Function MapPhysToLin Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysSize As Long, ByRef PhysMemHandle) As Long
Declare Function UnmapPhysicalMemory Lib "WinIo.dll" (ByVal PhysMemHandle, ByVal LinAddr) As Boolean
Declare Function GetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByRef PhysVal As Long) As Boolean
Declare Function SetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysVal As Long) As Boolean
Declare Function GetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByRef PortVal As Long, ByVal bSize As Byte) As Boolean
Declare Function SetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByVal PortVal As Long, ByVal bSize As Byte) As Boolean
Declare Function InitializeWinIo Lib "WinIo.dll" () As Boolean
Declare Function ShutdownWinIo Lib "WinIo.dll" () As Boolean
Declare Function InstallWinIoDriver Lib "WinIo.dll" (ByVal DriverPath As String, ByVal Mode As Integer) As Boolean
Declare Function RemoveWinIoDriver Lib "WinIo.dll" () As Boolean
Public IOStat As Boolean
'************************************************************
'*函数名称: GetPrnStat *
'*功能: 根据打印机的内存地址,检测打印机的目前工作状态 *
'*参数: *
'* lptport: 要检测的打印机的端口号,如LPT1: *
'*返回值: 打印机的工作状态值。 *
'* 0:正常 1:缺纸 2:无联系 3:异常(其他错误) *
'*调用:本模块中的API函数InitializeWinIo和GetPortVal *
'*备注:检测的内存地址,是在打印端口所在的基地址上加1; *
'*作者: 谷霖 *
'* LPT1口的基地址为&H378;LPT2口的基地址为&H278 *
'************************************************************
Public Function GetPrnStat(ByVal LptPort As String) As Long
Dim PrnAddr As Long
On Error Resume Next
If IOStat = False Then IOStat = InitializeWinIo()
If IOStat Then
If UCase(LptPort) = "LPT1:" Then
PrnAddr = &H379
ElseIf UCase(LptPort) = "LPT2:" Then
PrnAddr = &H279
End If
GetPortVal PrnAddr, GetPrnStat, 1
Else
GetPrnStat = &HFF
End If
GetPrnStat = GetPrnStat And &HF8
Select Case GetPrnStat
Case &H68, &H58, &H70
GetPrnStat = 1 '缺纸
Case &H78
GetPrnStat = 2 '无联系
Case &HD8
GetPrnStat = 0 '正常
Case Else
GetPrnStat = 3 '异常
End Select
End Function
'********************************************************************************************
'* 函数功能:检查打印机的状态主函数 *
'* 输入参数: *
'* PrintName 要检测的打印机名称 *
'* 输出参数: *
'* checkprinterr *
'* 检查结果(0:正常 1:打印机缺纸 2:打印机无联系 3:打印机异常 *
'* 4:没有安装打印机 5:打印机名称错误) *
'********************************************************************************************
Public Function CheckPrintErr(ByVal PrintName As String) As Long
'CheckPrintErr参数说明
'0:没有错误
'1:打印机无联系
'2:打印机缺纸
'3:没有安装打印机
Dim printjieguo As Long
Dim i As Long, k As Long
On Error GoTo ErrCheckPrint
If Printers.Count = 0 Then
CheckPrintErr = 4 '没有安装打印机
Exit Function
End If
'检测发票打印机是否可以联系
For i = 0 To Printers.Count - 1
If (Printers(i).DeviceName = PrintName) Then
k = k + 1
Exit For
End If
Next
If k = 0 Then '打印机名称错误
CheckPrintErr = 5
Exit Function
End If
Set Printer = Printers(i)
CheckPrintErr = GetPrnStat(Printer.Port)
Exit Function
ErrCheckPrint:
CheckPrintErr = 3
Exit Function
End Function