excel bva code to send command to usb printer

微笑、不失礼 提交于 2019-12-25 09:58:54

问题


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

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.

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.

Mike

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

unquote

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.

Tks in advance, Gabriel


回答1:


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.



来源:https://stackoverflow.com/questions/9829178/excel-bva-code-to-send-command-to-usb-printer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!