Three Methods to Send Preformatted Files Directly to PrinterID: Q119113
|
This article discusses three methods for sending data or a preformatted
file directly to the printer, thus bypassing the Windows printer driver:
Applications written for Windows version 3.1 can use either the SpoolFile()
function or the PASSTHROUGH printer escape to send a printer-specific file
to Print Manager for spooling to a printer. In addition, Visual Basic
allows you to open the printer port (lpt1, lpt2, etc.) as an MS-DOS device
and send data directly to it. The following sections discuss each method.
Q96795 : HOWTO: Use PASSTHROUGH Escape to Send Data Directly to Printer
Declare Function SpoolFile Lib "GDI" (ByVal lpszPrinter As String,
ByVal lpszPort As String,
ByVal lpszJob As String,
ByVal lpszFile As String)
As Integer
Declare Function GetProfileString Lib "Kernel" (ByVal lpAppName As String,
ByVal lpKeyName As String,
ByVal lpDefault As String,
ByVal lpReturnedString As
String,
ByVal nSize As Integer)
As Integer
Sub Command1_Click ()
Dim DefaultPrinter As String 'hold the default printer information
Dim Driver As String 'holds driver name
Dim Port As String 'holds printer port of driver
Dim FileToSpool As String 'name of file we are going to print
Dim ret As Integer 'return value of API calls
'initialize our printer buffer, call the API, and trim the Nulls
DefaultPrinter = Space$(128)
'the following statement needs to be on one line of code
ret = GetProfileString("windows", "device", "NONE", DefaultPrinter,
Len(DefaultPrinter))
DefaultPrinter = Left$(DefaultPrinter, ret)
'parse the printer line
'for the port, look for the second comma, this is where the port is
'for the printer driver, take the string up to the first comma
Driver = Left$(DefaultPrinter, InStr(DefaultPrinter, ",") - 1)
Port = Mid$(DefaultPrinter, InStr(Len(Driver) + 2, DefaultPrinter, ",")
+ 1)
'copy our data file and print the copy - SpoolFile will DELETE it!
FileToSpool = "c:\tmp\toprintr.prn"
FileCopy "c:\tmp\data.prn", FileToSpool 'the data file is data.prn
'spool the file to the PrintManager
ret = SpoolFile(Driver, Port, App.Title & "-Spooling File", FileToSpool)
'check for errors (values in the Win 3.1 SDK help file
If ((ret < -5) Or (ret > 0)) And (ret <> &H4000) Then
MsgBox "File Spooled-Don't Quit until PrintManager is done
printing!"
Else
MsgBox "Error on print: (" & CStr(ret) & ")"
End If
End Sub
Sub Command1_Click ()
Const MaxSize = 8192 'max buffer size
Dim Chunk As String 'buffer to hold data
Dim numLoops As Long 'number of 8k loops
Dim LeftOver As Integer 'amount of file left
Dim i As Integer 'counter for loops
'open our datafile and printer port
Open "c:\tmp\data.prn" For Binary As #1
Open "lpt1" For Binary As #2
'calculate size of file and amount left over
numLoops = LOF(1) \ MaxSize
LeftOver = LOF(1) Mod MaxSize
'initialize variables and loop
Chunk = Space$(MaxSize)
For i = 1 To numLoops
Get #1, , Chunk
Put #2, , Chunk
Next
'grab what's leftover
Chunk = Space$(LeftOver)
Get #1, , Chunk
Put #2, , Chunk
'close all our open files
Close #2
Close #1
End Sub
See the following Microsoft Knowledge Base article from the WinSDK
database:
Q111010 : HOWTO: Use PASSTHROUGH As An Alternative to SpoolFile()
Additional query words: kbVBp300 kbNoKeyWord kbVBp200
Keywords :
Version : WINDOWS:2.0,3.0
Platform : WINDOWS
Issue type :
Last Reviewed: May 12, 1999