excel vba代码向usb打印机发送命令

时间:2012-03-22 19:28:57

标签: usb printers

我正在尝试使用我的Epson TM U220,就像我在安装带有WIN 7 64位和Excel2010的新PC之前一样,没有并行端口。在过去,我使用这个简单的代码打开现金抽屉:

Sub drawer_opener()
Open "LPT1" For Output As #1
Print #1, Chr(27) + Chr(112) + Chr(0) + Chr(25) + Chr(250)
Close #1

End Sub

现在我正在使用适配器并行USB,但在更改" LPT1"之后没有响应打开现金抽屉commnad到" USB001"在代码中。 经过一些网络研究后,我发现以下代码可以正常工作,但它会因某种原因不断弹出错误消息。

我引用我发现的帖子并将所有信用都归还给#34; Mike" 引用 正如您所发现的那样,这种方法不会对USB打印机起作用。尝试 以下代替。我假设USB打印机已设置为 您的默认打印机(尽管可以添加要查找的代码 如果它不是),当然你也需要检查你的新 打印机遵循相同的控制代码,与旧的控制代码相同。

麦克

Option Explicit
Private Declare Function OpenPrinter Lib "winspool.drv" _
Alias "OpenPrinterA" (ByVal pPrinterName As String, _
phPrinter As Long, ByVal pDefault As Long) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv" _
Alias "StartDocPrinterA" (ByVal hPrinter As Long, _
ByVal Level As Long, pDocInfo As DOCINFO) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long
Private Declare Function WritePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long, pBuf As Any, _
ByVal cdBuf As Long, pcWritten As Long) As Long
Private Declare Function ClosePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long
Private Type DOCINFO
pDocName As String
pOutputFile As String
pDatatype As String
End Type

Private Sub Command1_Click()
Dim printerHandle As Long, retVal As Long
Dim bytesWritten As Long, lDoc As Long
Dim s1 As String, MyDocInfo As DOCINFO
retVal = OpenPrinter(Printer.DeviceName, printerHandle, 0)
If retVal = 0 Then
MsgBox "Printer Not found"
Exit Sub
End If
MyDocInfo.pDocName = "Any Name"
MyDocInfo.pOutputFile = vbNullString
MyDocInfo.pDatatype = vbNullString
lDoc = StartDocPrinter(printerHandle, 1, MyDocInfo)
Call StartPagePrinter(printerHandle)
s1 = Chr(27) + Chr(112) + Chr(0) + Chr(25) + Chr(250)
retVal = WritePrinter(printerHandle, ByVal s1, _
Len(s1), bytesWritten)
retVal = EndPagePrinter(printerHandle)
retVal = EndDocPrinter(printerHandle)
retVal = ClosePrinter(printerHandle)
End Sub

引文结束

我请求你们的一些专家请尝试代码,看看它是否对Excel2010不起作用,如果它实际上不是VBA或者是什么问题。此外,如果没有希望通过软件(代码)解决方案向Epson打印机发送命令,我必须在PC中安装并行端口并开始使用并行并行电缆。

提前说,加布里埃尔

1 个答案:

答案 0 :(得分:1)

更简单的方法:共享您的USB打印机,然后通过LPT1:将打印机共享映射到net use

net use LPT1: \\.\PrinterShareName

通过这种方式,您可以尝试使旧代码正常工作。