1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
Option Compare Database
Option Explicit
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrn As Long, ByVal Level As Long, pDocInfo As DOC_INFO_1) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
Private Declare Function WritePrinter Lib "winspool.drv" (ByVal hPrn As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long) As Long
Private Type DOC_INFO_1
pDocName As String
pOutputFile As String
pDatatype As String
End Type
' RawDataToPrinter - sends binary data directly to a printer
'
' Params:
' szPrinterName - NULL terminated string specifying printer name
' lpData - Pointer to raw data bytes
' dwCount - Length of lpData in bytes
'
' Returns: TRUE for success, FALSE for failure.
'
Public Function RawDataToPrinter(szPrinterName As String, lpData As String, dwCount As Long) As Boolean
Dim hPrinter As Long
Dim DocInfo As DOC_INFO_1
Dim dwJob As Long
Dim dwBytesWritten As Long
' Need a handle to the printer.
If OpenPrinter(szPrinterName, hPrinter, ByVal 0&) = 0 Then
RawDataToPrinter = False
Exit Function
End If
' Fill in the structure with info about this "document."
DocInfo.pDocName = "Agrafage"
DocInfo.pOutputFile = vbNullString
DocInfo.pDatatype = "RAW"
' Inform the spooler the document is beginning.
dwJob = StartDocPrinter(hPrinter, 1, DocInfo)
If dwJob = 0 Then
ClosePrinter (hPrinter)
RawDataToPrinter = False
Exit Function
End If
' Start a page.
If StartPagePrinter(hPrinter) = 0 Then
EndDocPrinter (hPrinter)
ClosePrinter (hPrinter)
RawDataToPrinter = False
Exit Function
End If
' Send the data to the printer.
If WritePrinter(hPrinter, lpData, dwCount, dwBytesWritten) = 0 Then
EndPagePrinter (hPrinter)
EndDocPrinter (hPrinter)
ClosePrinter (hPrinter)
RawDataToPrinter = False
Exit Function
End If
' End the page.
If EndPagePrinter(hPrinter) = 0 Then
EndDocPrinter (hPrinter)
ClosePrinter (hPrinter)
RawDataToPrinter = False
Exit Function
End If
' Inform the spooler that the document is ending.
If EndDocPrinter(hPrinter) = 0 Then
ClosePrinter (hPrinter)
RawDataToPrinter = False
Exit Function
End If
' Tidy up the printer handle.
ClosePrinter (hPrinter)
'Check to see if correct number of bytes were written.
If dwBytesWritten <> dwCount Then
RawDataToPrinter = False
Exit Function
End If
RawDataToPrinter = True
End Function
Private Sub Test()
Dim Texte As String
Texte = "@PJL SET COPIES = 3" & Chr(13) & Chr(10) & "@PJL SET ORIENTATION = LANDSCAPE" & Chr(13) & Chr(10)
If RawDataToPrinter("PDFCreator", Texte, Len(Texte)) Then
MsgBox "OK!"
Else
MsgBox "NOK!"
End If
End Sub |
Partager