Ho copiato il codice da te gentilmente linkato in un modulo (togliendo le righe che mi sembravano superflue per la prova), poi ho fatto un form apposito con il solo bottone stampa che fa riferimento a un file di testo di prova, ma mi da errore su hPrn dice che è un tipo non corrispondente perché devo ancora capire come è gestito dai 3 call che lo usano, ma sembrerebbe essere sulla strada giusta.
'INIZIO MODULO
Option Compare Database
Option Explicit
Const PRINTER_ENUM_DEFAULT = &H1
Declare PtrSafe Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As LongPtr, pDefault As Any) As Long
Declare PtrSafe Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As LongPtr, ByVal Level As Long, pDocInfo As DOC_INFO_1) As Long
Declare PtrSafe Function StartPagePrinter Lib "winspool.drv" (ByVal hPrinter As LongPtr) As Long
Declare PtrSafe Function WritePrinter Lib "winspool.drv" (ByVal hPrinter As LongPtr, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long) As Long
Declare PtrSafe Function EndPagePrinter Lib "winspool.drv" (ByVal hPrinter As LongPtr) As Long
Declare PtrSafe Function EndDocPrinter Lib "winspool.drv" (ByVal hPrinter As LongPtr) As Long
Declare PtrSafe Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As LongPtr) As Long
Private Type DOC_INFO_1
pDocName As String
pOutputFile As String
pDatatype As String
End Type
Public Sub SpoolFile(sFile As String, PrnName As String, Optional AppName As String = "Magazzino")
Dim hPrn As Long
Dim buffer() As Byte
Dim hFile As Integer
Dim Written As Long
Dim di As DOC_INFO_1
Dim i As Long
Const BufSize As Long = &H4000
'Extract filename from passed spec, and build job name.
'Fill remainder of DOC_INFO_1 structure.
If InStrRev(sFile, "\") Then
di.pDocName = Mid$(sFile, InStrRev(sFile, "\") + 1)
Else
di.pDocName = sFile
End If
If Len(AppName) Then
di.pDocName = AppName & ": " & di.pDocName
End If
di.pOutputFile = vbNullString
di.pDatatype = "RAW"
'Open printer for output to obtain handle.
'Set it up to begin recieving raw data.
Call OpenPrinter(PrnName, hPrn, ByVal 0&)
Call StartDocPrinter(hPrn, 1, di)
Call StartPagePrinter(hPrn)
'Open file and pump it to the printer.
hFile = FreeFile
Open sFile For Binary Access Read As hFile
'Read in 16K buffers and spool.
ReDim buffer(1 To BufSize) As Byte
For i = 1 To LOF(hFile) \ BufSize
Get #hFile, , buffer
Call WritePrinter(hPrn, buffer(1), BufSize, Written)
Next i
' Get last chunk of file if it doesn't
' fit evenly into a 16K buffer.
If LOF(hFile) Mod BufSize Then
ReDim buffer(1 To (LOF(hFile) Mod BufSize)) As Byte
Get #hFile, , buffer
Call WritePrinter(hPrn, buffer(1), UBound(buffer), Written)
End If
Close #hFile
' Shut down spooling process.
Call EndPagePrinter(hPrn)
Call EndDocPrinter(hPrn)
Call ClosePrinter(hPrn)
Kill sFile
End Sub
'FINE MODULO
'INIZIO FORM
Private Sub CmdPrint_Click()
Dim io As Integer
io = FreeFile
Open "Esempio.txt" For Output As io
Print #io, "Riga di stampa1"
Print #io, "Riga di stampa2"
Close #io
Call SpoolFile("Esempio.txt", "Zebra Technologies ZTC iMZ320")
End Sub
'FINE FORM