Print File VB6

I would like to be able to print a text file from VB6 and also specify which printer to use.  I would like to be able to do this without opening the file and bringing the data into VB and then using the printer object.  I would like some sort of function that I can print a file to a specified printer.  Does such a function exist?

Thanks
Fraser_AdminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mikal613Commented:
well to chose your printer

you do

Load Printers
-----------
Dim P As Printer

    For Each P In Printers
        If P.DeviceName = cboPrinter.Text Then
            combo1.additem p.Devicename & "  " & p.Port
           
         
            Exit For
        End If
    Next

setPrintert
-------------
Dim P As Printer

    For Each P In Printers
        If P.DeviceName = cboPrinter.Text Then
            Set Printer = P
           
         
            Exit For
        End If
    Next

Something like this basically you can loop

0
Mikal613Commented:
It's not elegant, but you can use the shell comand to type your file to lpt1: shell "Type "&filnam$&">LPT1"
0
Mikal613Commented:
or a better way is

Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

 call ShellExecute(0, "print", "myfile.ext", "", "", 1)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Fraser_AdminAuthor Commented:
This is what is happening.  I know it is bizarre, but, when using the printer.print method to print my ticket, it is causing the paper to jam sometimes (very periodic).  We can prove this by sending it to the printer about 20 times.  I can do the same with a Windows test page and the printer never jams.  So what I'm looking to do is create a file in my VB code, print the file and then delete it to see if the printer will still jam.

1) When I use shellExecute, it continues running the next statement.  Therefore my document is deleted before it is printed and it cannot find the file.
2) When I use shellExecute, I cannot specify the printer, which I need to be able to do.  I need to set the printer to an actual name.  I tried setting the printer.devicename, but it still uses my default printer.
3) I cannot use LPT, I need to use actual names of printers.
0
Mikal613Commented:
setPrintert
-------------
Dim P As Printer

    For Each P In Printers
        If P.DeviceName = cboPrinter.Text Then  'Set your printer here
            Set Printer = P
           
         
            Exit For
        End If
    Next

Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

 call ShellExecute(0, "print", "myfile.ext", "", "", 1)
0
Mikal613Commented:
then call shell execute after you set your printer
0
Fraser_AdminAuthor Commented:
I've done this.  It still prints to the default printer.
0
Mikal613Commented:
It's not elegant, but you can use the shell comand to type your file to lpt1: shell "Type "&filnam$&">LPT1"

">LPT1""  can be any printer just write the port that your printer sits
0
Fraser_AdminAuthor Commented:
I'm using Networked Printers.  I need to specify a name.
0
Fraser_AdminAuthor Commented:
That does not work.  It sends nothing to the printer?  No errors, just does nothing.
0
Mikal613Commented:
Is the printer on?
0
Fraser_AdminAuthor Commented:
Yes.  I can't get either one of your links to work.  If I do...
lr = ShellExecute(hWnd, "Print", FileName, "", "", 0)

This will print, the flow of the program does not stop therefore the file I'm sending is deleted before it even gets printed.  If I comment out the delete, it works, but I need to print to a small receipt printer, and it is trying to print it from notepad, and then put 2cm margins.  I can't have margins that big.  I also need to be able to delete the file after it is printed.
0
Mikal613Commented:
post your code with Printer name please
0
Fraser_AdminAuthor Commented:
CUSTOMER_PRINTER_NAME is set to Xerox 55 PS earlier.  The spoolfile sub-routine does not seem to do anything.
SpoolFile "C:\Data\Scales-May4\" & txtScaleBill.Text & ".dat", CUSTOMER_PRINTER_NAME

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
Mikal613Commented:
ok try this

set yourprinter with my previous Attempts and then call this little function

 Public Sub PrintTXTFile(FileName As String)
    Dim x As Integer
    Dim s As String

    x = FreeFile
    On Error GoTo HandleError
    Open FileName For Input As x
    Do While Not EOF(x)
        Line Input #x, s
        Printer.Print s
    Loop
    Printer.EndDoc
    Close #x
    Exit Sub
HandleError:
    MsgBox "Error :" & Err.Description, vbCritical, "Printing File..."
End Sub
0
Fraser_AdminAuthor Commented:
I also tried:

Set FS = CreateObject("Scripting.FileSystemObject")
FS.CopyFILE "C:\Data\Scales-May4\" & txtScaleBill.text & ".dat", "Xerox 55 PS"
Set FS = Nothing


Xerox 55 PS is the name of the printer in my printers list.
0
Fraser_AdminAuthor Commented:
I can't use printer.print.  That is why I'm going through all this maddness.  I have a printer that is jamming only when printing from my vb app (which uses printer.print).  It never jams from Windows test page.  I need to try to create a file and then send it to a printer.  I will do this multiple times to see if when I fill up the spooler it jams or not.
0
Mikal613Commented:
you have the Printer name wrong

On your list of printers  from control panel you shopuld have \\Somthing\Printer
0
Fraser_AdminAuthor Commented:
I don't.  I just have the printer name like that.  I'm not sure how it was setup, but that is what my printer name looks like.  This is just the printer I'm testing with.  In production the actual printer I'm using is setup like that.
0
Mikal613Commented:
well you need the Share name to print to it
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fraser_AdminAuthor Commented:
The printer I'm testing with is setup from an IP address.
0
JpmSoftCommented:
Use Simple TCP Communications...

Connect to Printer's IP Address using winsock at port 9100 (this is the standar, your printer may use another one)  and then start sending the data file through this connection...

This will work...

You can even try LPR communication but it's more complicated... check

http://www.faqs.org/rfcs/rfc1179.html

0
Fraser_AdminAuthor Commented:
I'm not going to use printing to a file.  I think I've finally got the printer manufactur to help me debug why the printer is jamming.

Thanks for all your suggestions.  I will give you the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.