CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Feb 2004
    Posts
    59

    Question how to print existing .txt file?

    I have a txt file.
    I know the path to the txt file on the local machine. (c:\textFile.txt)

    How do I print this text file?

    Thanks.

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    A trick: copyFile to printer

    You can use Fso to copy file to printer, like in this article:
    http://support.microsoft.com/default...b;en-us;252607

    And you could also try copying a file to LPT1
    (some other tricks with lpt1:
    http://www.codeguru.com/forum/showth...ighlight=print )

    Or: you read the file and print the variable
    Last edited by Cimperiali; March 29th, 2004 at 11:21 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Feb 2004
    Posts
    59
    I tried:
    Code:
    Dim FSO as FileSystemObject
    set FSO = new FileSystemObject
    
    FSO.CopyFile "C:\test.txt", Printer.DeviceName
    I didn't get an error but I also didn't see any results.

    I tried:

    Code:
    Dim strTemp As String
        Open "c:\text.txt" For Input As #1
      
        Do Until EOF(1)
            Line Input #1, strTemp
            Printer.Print strTemp
        Loop
    and this works. Is there a cleaner way to do this print - a method that doesn't involve looping thru each line?

    Secondly, is there a way to name the printed out file (as I'm printing to the pdfwriter)?

    Thirdly, is there a way to set where the pdf printed file is saved to?

    Thanks.
    Tiff

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    about first: you can load all file in a var in a single step
    (see Cakkie Faq-the sticy post), and print the var with a single
    print instruction.
    Do not know about second and third
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Feb 2004
    Posts
    59

    another solution:

    I'm now using:
    Code:
    Dim strTemp As String
    Open "textFile.txt" For Input As #1
    strTemp = Input(FileLen("textFile.txt"), 1)
    Printer.Print strTemp
    and it is working properly. Thank you!

  6. #6
    Join Date
    Nov 2002
    Location
    New Delhi
    Posts
    9
    using printer object.

  7. #7
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Code:
    Dim strTemp As String
    Open "textFile.txt" For Input As #1
    strTemp = getStringContentAtOnce("textFile.txt")
    Printer.Print strTemp
    Printer.EndDoc
    
    '....
    Public Function getStringContentAtOnce(ByVal FileName As String) As String
    
        Dim intFree As Integer
          
        ' ensure that the file exists
        If Len(Dir$(FileName)) = 0 Then
            Err.Raise 53 ' File not found
        End If
          
        ' open in binary mode
        intFree = FreeFile
        Open FileName$ For Binary As #intFree
        ' read the string and close the file
        getStringContentAtOnce = Space$(LOF(intFree))
        Get #intFree, , getStringContentAtOnce
        Close #intFree
    End Function
    Last edited by Cimperiali; April 22nd, 2004 at 03:39 PM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  8. #8
    Join Date
    Feb 2001
    Location
    PA
    Posts
    163
    Here's anlother way...

    Dim PrintIt
    Dim PrintText As String

    PrintText = "C:\textfile,txt"
    PrintIt = Shell("c:\winnt\system32\notepad.exe /p " & PrintText, 6)

  9. #9
    Join Date
    Feb 2004
    Posts
    59
    Cimperiali - thanks for the code. I'm using:
    Code:
    Dim strTemp As String
    Open "textFile.txt" For Input As #1
        strTemp = getStringContentAtOnce("textFile.txt")
        Printer.Print strTemp
        Printer.EndDoc
        Close #1
    
    Public Function getStringContentAtOnce(ByVal FileName As String) As String
    
        Dim intFree As Integer
          
        ' ensure that the file exists
        If Len(Dir$(FileName)) = 0 Then
            Err.Raise 53  ' File not found
        End If
          
        ' open in binary mode
        intFree = FreeFile
        Open FileName$ For Binary As #intFree
        ' read the string and close the file
        getStringContentAtOnce = Space$(LOF(intFree))
        Get #intFree, , getStringContentAtOnce
        Close #intFree
    End Function
    and I'm getting: Run-time error '55': File already open - on the Open FileName$ For Binary As #intFree line. Do you know why?

    Cubbie - what variable type is PrintIt? What should the code do?

    Thanks for your help.
    Tiff

  10. #10
    Join Date
    Feb 2004
    Posts
    59

    It works!

    I took out the first set of open and close code and it works:

    Code:
        
    Dim strTemp As String
    
    strTemp = getStringContentAtOnce("textFile.txt") 
        Printer.Print strTemp
        Printer.EndDoc
    
    
    Public Function getStringContentAtOnce(ByVal FileName As String) As String
    
        Dim intFree As Integer
          
        ' ensure that the file exists
        If Len(Dir$(FileName)) = 0 Then
            Err.Raise 53  ' File not found
        End If
          
        ' open in binary mode
        intFree = FreeFile
        Open FileName$ For Binary As #intFree
        ' read the string and close the file
        getStringContentAtOnce = Space$(LOF(intFree))
        Get #intFree, , getStringContentAtOnce
        Close #intFree
    End Function
    Thank you!

  11. #11
    Join Date
    Feb 2004
    Posts
    59

    run-time errors

    For each solution that I've used below, sometimes I'll get errors. I'll use the same text file - sometimes it prints, sometimes it returns an error.

    The most recent code worked fine yesterday.

    Today (using the same file) I'm getting "Run-time error '482': Printer error" - for the line: Printer.Print strTemp

    What's causing this error? Why is there sometimes an error and sometimes not?

  12. #12
    Join Date
    Feb 2001
    Location
    PA
    Posts
    163
    I used a variant but a Long would probably be more appropriate

  13. #13
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Might be some non printable chars?

    Might it be in your variable you get some chars that are not accepted by printer?
    For example, in my office there is a printer that usally does not accept pictures....But software does not crash - the Printer does crash...
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  14. #14
    Join Date
    Nov 2002
    Location
    New Delhi
    Posts
    9
    Code:
    'this will print a text file , it's ok and tested i have using in projects for fast printing
    'for any problem pls inform
    
    private sub Command1_Click()
        PrintTheFile "c:\mytextfile.txt"
    end sub
    
    Public Sub PrintTheFile(FileToPrint As String)
       Dim sTemp1 As String, sTemp2 As String, ii As Long
       dim jj As Integer, i As Integer
       Close #1
       Close #2
       Open FileToPrint For Binary As #1: Open Printer.Port For Binary As #2
       ii = LOF(1) \ 8192
       jj = LOF(1) Mod 8192
      sTemp2 = String(8192, " ")
       For i = 1 To ii
          sTemp1 = sTemp2
         Get #1, , sTemp1
        Put #2, , sTemp1
    Next
       sTemp1 = String(jj, " ")
       Get #1, , sTemp1
       Put #2, , sTemp1
       Close #1
       Close #2
    End Sub
    Last edited by Cimperiali; April 8th, 2004 at 02:21 AM.

  15. #15
    Join Date
    Feb 2004
    Posts
    59

    follow-ups and new questions

    jkpradyumn:

    Thanks for the code. I tried using it. I'm printing to Acrobat PDFWriter. The text file is sent to the correct printer and is queued. Then the printer status changes to Error-Printing. Is there a way to modify your code so that it will work with Acrobat PDFWriter?

    *****
    Cimperiali:

    The text file only has alpha characters plus some keyboard symbols. Processing the same file - sometimes it works, sometimes it doesn't...

    *****
    Since the file it being printed to Acrobat PDFWriter a new file is being created. Is there a way set the default folder the file is saved to? I tried using commonDialog1.InitDir = "C:\" but that didn't do anything.

    Thank you for your time!!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured