Excel VBA 中访问打印机对象的替代方法 [英] Alternative to Access Printer object in Excel VBA

查看:65
本文介绍了Excel VBA 中访问打印机对象的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将原始数据 (ZPL) 发送到 Excel VBA 中的选定打印机.我已经在不同的 Ms Access 项目中成功地做到了这一点,但现在我需要在 Excel 项目中使用相同的功能.我可以通过在我的 Excel VBA 项目中引用 Access 对象库来使用相同的代码,并且它按预期工作.问题是工作簿以后可以在没有安装Access的电脑上使用,我找不到替代对象.

I am trying to send raw data (ZPL) to a selected printer in Excel VBA. I have done this successfully in different Ms Access projects, but now I need the same functionality in an Excel project. I am able to use the same code by referencing the Access object library in my Excel VBA project and it works as expected. The problem is that the workbook can later be used on computers without Access installed, and I can't find the alternative objects.

Private Type DocInfo
    pDocName As String
    pOutputFile As String
    pDatatype As String
End Type

Private Declare PtrSafe Function ClosePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long) As Long
Private Declare PtrSafe Function EndDocPrinter Lib "winspool.drv" (ByVal 
_
   hPrinter As Long) As Long
Private Declare PtrSafe Function EndPagePrinter Lib "winspool.drv" (ByVal 
_
   hPrinter As Long) As Long
Private Declare PtrSafe Function OpenPrinter Lib "winspool.drv" Alias _
   "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
    ByVal pDefault As Long) As Long
Private Declare PtrSafe Function StartDocPrinter Lib "winspool.drv" Alias 
_
   "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
   pDocInfo As DocInfo) As Long
Private Declare PtrSafe Function StartPagePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long) As Long
Private Declare PtrSafe Function WritePrinter Lib "winspool.drv" (ByVal _
   hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, _
   pcWritten As Long) As Long    


Public Function RawPrint(strData As String, ByVal SelectedPrinter As String)
Dim defprt As Printer

Dim lhPrinter As Long
Dim lReturn As Long
Dim lpcWritten As Long
Dim lDoc As Long
Dim sWrittenData As String
Dim mDocInfo As DocInfo
lReturn = OpenPrinter(SelectedPrinter, lhPrinter, 0)

If lReturn = 0 Then
    MsgBox "The Printer is not recognized."
    Exit Function
End If
mDocInfo.pDocName = "ZPl"
mDocInfo.pOutputFile = vbNullString
mDocInfo.pDatatype = vbNullString
lDoc = StartDocPrinter(lhPrinter, 1, mDocInfo)
Call StartPagePrinter(lhPrinter)
sWrittenData = strData
lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _
Len(sWrittenData), lpcWritten)
lReturn = EndPagePrinter(lhPrinter)
lReturn = EndDocPrinter(lhPrinter)
lReturn = ClosePrinter(lhPrinter)



Exit_RawPrint:
Exit Function



 End Function

如果没有引用 Access 对象库,我在Dim defprt As Printer"上遇到错误 - Excel 对象库中似乎没有像 Printer 这样的对象.有没有其他方法可以访问 Excel 中的打印机?任何指向该方向的指针都会非常有帮助.感谢您抽出宝贵时间.

Without the Access Object Library referenced, I am getting error on "Dim defprt As Printer" - there seems to be no such object as Printer in the Excel object library. Is there any alternative way to access the printers in Excel? Any pointers in that direction would be very helpful. Thanks for your time.

推荐答案

你根本没有对那个打印机对象做任何事情.只需省略该行.

You're not doing anything at all with that printer object. Just omit the line.

您似乎已经找到了使用 WinAPI 连接到打印机的必要代码,所以我不确定您希望我们做什么.

It seems you've already figured out the necessary code to connect to a printer using WinAPI, so I'm not sure what you want us to do.

我确实相信您的 hPrinter 应该是 LongPtr,但只要您使用的是 32 位 Access,它们都是一样的.

I do believe your hPrinter should be a LongPtr, but as long as you're using 32-bits Access, they're the same.

这篇关于Excel VBA 中访问打印机对象的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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