如何在 VBA 中使用 VB 打印机对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6377421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:29:52  来源:igfitidea点击:

How to use VB Printer object in VBA

ms-accessvbaprinting

提问by rsleiman

I'm trying to print to an Epson printer using VBA code without success. Using VB6, I have the following code which it works just fine:

我正在尝试使用 VBA 代码打印到 Epson 打印机,但没有成功。使用 VB6,我有以下代码,它工作得很好:

Printer.Print "Hello"
Printer.EndDoc 

My problem is that I don't see the Printer object in VBA (using MS Access Macros). Do I have to include a specific reference? If yes, what it would be? such that I have VB6 runtime installed on the same machine.

我的问题是我在 VBA 中没有看到打印机对象(使用 MS Access 宏)。我是否必须包含特定的参考?如果是,那会是什么?这样我就在同一台机器上安装了 VB6 运行时。

回答by Renaud Bompuis

You can't do this from MS Access, but KB154078shows VBA code that can use the Win32 API to communicate directly with the print spooler and send raw data to a printer:

您无法从 MS Access 执行此操作,但KB154078显示了可以使用 Win32 API 直接与打印后台处理程序通信并将原始数据发送到打印机的 VBA 代码:

  Option Explicit

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

  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 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 Sub Command1_Click()
      Dim lhPrinter As Long
      Dim lReturn As Long
      Dim lpcWritten As Long
      Dim lDoc As Long
      Dim sWrittenData As String
      Dim MyDocInfo As DOCINFO
      lReturn = OpenPrinter(Printer.DeviceName, lhPrinter, 0)
      If lReturn = 0 Then
          MsgBox "The Printer Name you typed wasn't recognized."
          Exit Sub
      End If
      MyDocInfo.pDocName = "AAAAAA"
      MyDocInfo.pOutputFile = vbNullString
      MyDocInfo.pDatatype = vbNullString
      lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
      Call StartPagePrinter(lhPrinter)
      sWrittenData = "How's that for Magic !!!!" & vbFormFeed
      lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _
         Len(sWrittenData), lpcWritten)
      lReturn = EndPagePrinter(lhPrinter)
      lReturn = EndDocPrinter(lhPrinter)
      lReturn = ClosePrinter(lhPrinter)
  End Sub

Another example is given in KB175083.

另一个例子在KB175083 中给出。