excel bva 代码将命令发送到 USB 打印机 [英] excel bva code to send command to usb printer

查看:52
本文介绍了excel bva 代码将命令发送到 USB 打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的 Epson TM U220,就像我在安装带有 WIN 7 64 位和 Excel2010 的新 PC 之前所做的那样,没有并行端口.过去我用这个简单的代码打开钱箱:

I am trying to use my Epson TM U220 as I did before installing a new PC with WIN 7 64bit and Excel2010, no parallel port. In the past I used this simple code to open cash drawer:

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"更改为USB001"后,打开现金抽屉命令没有任何响应.经过一些网络研究,我发现以下代码应该可以工作,但由于某种原因,它不断弹出错误消息.

Now I am using an adapter parallel-USB but with no response to opening cash drawer commnad after changing "LPT1" to "USB001" in code. After some web research I found following code that is supposed to work, but it keeps popping error messages for one reason or the other.

我引用我发现的线程并将所有功劳归功于迈克"引用正如您所发现的,这种方法不适用于 USB 打印机.尝试下面代替.我假设 USB 打印机设置为您的默认打印机(虽然可以添加代码来查找如果不是的话)当然你还需要检查你的新打印机以与旧打印机相同的方式遵守相同的控制代码.

I quote thread I found and give all the credit to "Mike" quote As you've discovered, that approach won't work on USB printers. Try the following instead. I've assumed that the USB printer is set up as your default printer (although it is possible to add code to look for it if it is not) and of course you'll also need to check that your new printer obeys the same control codes in the same way as your old one.

迈克

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 上安装一个并口并开始像过去一样使用并并电缆.

I kindly request that some expert of you guys would please try the code and see if it just doesnt work for Excel2010, if it is not actually VBA or what is the problem. Also if there is no hope via software (code) solution to send command to Epson printer and I have to install a parallel port in PC and start using a parallel-parallel cable as in the past.

提前确认,加布里埃尔

推荐答案

更简单的方法:共享您的 USB 打印机,然后通过 net useLPT1:>:

Simpler approach: Share your USB Printer, and then map the printer share to LPT1: via net use:

net use LPT1: \\.\PrinterShareName

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

This way you can try to make your old code work.

这篇关于excel bva 代码将命令发送到 USB 打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆