Printing a file

Posted on 1999-01-12
Last Modified: 2010-05-03
My question is, after i have print (any doc) to can i can use vb to send these files to printer directly ?
Like in ms-dos command where we can use "copy" command to do that. Please help. Thanks
Question by:edkoo
LVL 13

Expert Comment

ID: 1455773
Using the ShellExecute API and using "Print" as command.

Expert Comment

ID: 1455774
You have two methods of printing a file directly to the printed without going through an application layer.. similar to dos copy file lpt1:

Method 1

        Printer.FontName = "Courier New"
        Printer.FontSize = "10"
        'Open your file
        'While not at the end of your file
             'Read a line from the file into var LineFromFile
             Printer.print LineFromFile

       If you like I can give you the complete code including how to select the printer..

Method 2:

    Shell("cmd /k copy yourfilename.ext lpt1")

Let me know
LVL 14

Accepted Solution

waty earned 50 total points
ID: 1455775
I have just writing (this morning) a process to do that here.
Here is the code you need :

' #VBIDEUtils#************************************************************
' * Programmer Name  : Waty Thierry
' * Web Site         :
' * E-Mail           :
' * Date             : 12/01/99
' * Time             : 12:11
' * Module Name      : Spool
' * Module Filename  : Spool.bas
' **********************************************************************
' * Comments         : Spool a file directly to a printer
' *
' *
' **********************************************************************

Option Explicit

' *** Constants for DEVMODE structure
Public Const CCHDEVICENAME = 32
Public Const CCHFORMNAME = 32

Public Type DEVMODE
   dmDeviceName      As String * CCHDEVICENAME
   dmSpecVersion     As Integer
   dmDriverVersion   As Integer
   dmSize            As Integer
   dmDriverExtra     As Integer
   dmFields          As Long
   dmOrientation     As Integer
   dmPaperSize       As Integer
   dmPaperLength     As Integer
   dmPaperWidth      As Integer
   dmScale           As Integer
   dmCopies          As Integer
   dmDefaultSource   As Integer
   dmPrintQuality    As Integer
   dmColor           As Integer
   dmDuplex          As Integer
   dmYResolution     As Integer
   dmTTOption        As Integer
   dmCollate         As Integer
   dmFormName        As String * CCHFORMNAME
   dmLogPixels       As Integer
   dmBitsPerPel      As Long
   dmPelsWidth       As Long
   dmPelsHeight      As Long
   dmDisplayFlags    As Long
   dmDisplayFrequency As Long
   dmICMMethod       As Long        ' // Windows 95 only
   dmICMIntent       As Long        ' // Windows 95 only
   dmMediaType       As Long        ' // Windows 95 only
   dmDitherType      As Long        ' // Windows 95 only
   dmReserved1       As Long        ' // Windows 95 only
   dmReserved2       As Long        ' // Windows 95 only
End Type

   pDatatype         As Long
   pDevMode          As DEVMODE
   DesiredAccess     As Long
End Type

' *** Constants for DesiredAccess member of PRINTER_DEFAULTS

' *** Win32 API Calls
Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrn As Long, pDefault As PRINTER_DEFAULTS) 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 Declare Function EndPagePrinter 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 ClosePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long

' *** Structure required by StartDocPrinter
Private Type DOC_INFO_1
   pDocName          As String
   pOutputFile       As String
   pDatatype         As String
End Type

Public Function SpoolFile(sFile As String, sPrinterName As String, Optional sAppName As String = "") As Boolean
   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : Waty Thierry
   ' * Web Site         :
   ' * E-Mail           :
   ' * Date             : 12/01/99
   ' * Time             : 12:06
   ' * Module Name      : Spool
   ' * Module Filename  : Spool.bas
   ' * Procedure Name   : SpoolFile
   ' * Parameters       :
   ' *                    sFile As String
   ' *                    sPrinterName As String
   ' *                    Optional sAppName As String = ""
   ' **********************************************************************
   ' * Comments         : Spool a file on a printer
   ' *
   ' *
   ' **********************************************************************
   Dim hPrn       As Long
   Dim BBuffer()  As Byte
   Dim hFile      As Integer
   Dim NWritten   As Long
   Dim di         As DOC_INFO_1
   Dim i          As Long
   Const nBufSize As Long = &H4000
   On Error GoTo ERROR_SpoolFile
   SpoolFile = True
   ' *** Extract filename from passed spec, and build job name.
   ' *** Fill remainder of DOC_INFO_1 structure.
   If InStr(sFile, "\") Then
      For i = Len(sFile) To 1 Step -1
         If Mid(sFile, i, 1) = "\" Then Exit For
         di.pDocName = Mid(sFile, i, 1) & di.pDocName
      di.pDocName = sFile
   End If
   If Len(sAppName) Then
      di.pDocName = sAppName & ": " & 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.
   ' *** Set the PRINTER_DEFAULTS members
   pd.pDatatype = 0&
   pd.DesiredAccess = PRINTER_ALL_ACCESS
   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 BBuffers and spool.
   ReDim BBuffer(1 To nBufSize) As Byte
   For i = 1 To LOF(hFile) \ nBufSize
      Get #hFile, , BBuffer
      Call WritePrinter(hPrn, BBuffer(1), nBufSize, NWritten)
   ' *** Get last chunk of file if it doesn't
   ' *** fit evenly into a 16K BBuffer.
   If LOF(hFile) Mod nBufSize Then
      ReDim BBuffer(1 To (LOF(hFile) Mod nBufSize)) As Byte
      Get #hFile, , BBuffer
      Call WritePrinter(hPrn, BBuffer(1), UBound(BBuffer), NWritten)
   End If
   Close #hFile
   ' *** Shut down spooling process.
   Call EndPagePrinter(hPrn)
   Call EndDocPrinter(hPrn)
   Call ClosePrinter(hPrn)
   Exit Function
   SpoolFile = False
   Exit Function
End Function


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now