[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Print File VB6

Posted on 2004-11-30
25
Medium Priority
?
5,142 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:Fraser_Admin
  • 13
  • 11
25 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 12705672
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 12705687
It's not elegant, but you can use the shell comand to type your file to lpt1: shell "Type "&filnam$&">LPT1"
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12705705
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Fraser_Admin
ID: 12705763
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 12705778
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 12705780
then call shell execute after you set your printer
0
 

Author Comment

by:Fraser_Admin
ID: 12705839
I've done this.  It still prints to the default printer.
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12705890
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
 

Author Comment

by:Fraser_Admin
ID: 12705969
I'm using Networked Printers.  I need to specify a name.
0
 

Author Comment

by:Fraser_Admin
ID: 12706135
That does not work.  It sends nothing to the printer?  No errors, just does nothing.
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12706165
Is the printer on?
0
 

Author Comment

by:Fraser_Admin
ID: 12706238
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 12706297
post your code with Printer name please
0
 

Author Comment

by:Fraser_Admin
ID: 12706318
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 12706341
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
 

Author Comment

by:Fraser_Admin
ID: 12706357
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
 

Author Comment

by:Fraser_Admin
ID: 12706380
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 12706406
you have the Printer name wrong

On your list of printers  from control panel you shopuld have \\Somthing\Printer
0
 

Author Comment

by:Fraser_Admin
ID: 12706485
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
 
LVL 48

Accepted Solution

by:
Mikal613 earned 2000 total points
ID: 12706547
well you need the Share name to print to it
0
 

Author Comment

by:Fraser_Admin
ID: 12706550
The printer I'm testing with is setup from an IP address.
0
 
LVL 4

Expert Comment

by:JpmSoft
ID: 12718039
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
 

Author Comment

by:Fraser_Admin
ID: 12729436
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Simple Linear Regression
Six Sigma Control Plans

873 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