Solved

Printing a file

Posted on 1999-01-12
3
213 Views
Last Modified: 2010-05-03
My question is, after i have print (any doc) to file..how 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
0
Comment
Question by:edkoo
3 Comments
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1455773
Using the ShellExecute API and using "Print" as command.
0
 
LVL 3

Expert Comment

by:idt
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
        Printer.NewPage
        Printer.EndDoc

       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
0
 
LVL 14

Accepted Solution

by:
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         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * 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

Public Type PRINTER_DEFAULTS
   pDatatype         As Long
   pDevMode          As DEVMODE
   DesiredAccess     As Long
End Type

' *** Constants for DesiredAccess member of PRINTER_DEFAULTS
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const PRINTER_ACCESS_ADMINISTER = &H4
Public Const PRINTER_ACCESS_USE = &H8
Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
   PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)

' *** 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         : www.geocities.com/ResearchTriangle/6311/
   ' * E-Mail           : waty.thierry@usa.net
   ' * 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
      Next
   Else
      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
   Dim pd As PRINTER_DEFAULTS
   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)
   Next
   
   ' *** 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
   
ERROR_SpoolFile:
   SpoolFile = False
   Exit Function
   
End Function

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

707 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

18 Experts available now in Live!

Get 1:1 Help Now