Solved

print a txt file using textstream Line input  ... How to

Posted on 2003-11-13
4
1,066 Views
Last Modified: 2012-06-27
Hi all. What I am about to do may seem odd but I'm not sure how else to do it. Earlier I was trying to find a thread that discussed how to print a text file. I found a ton of code, but the whole:

Open strFilePath For Input As #1
While not Eof(1)
  Line Input #1, MyLine
  Printer.Print MyLine & vbcrlf
wend
Printer.EndDoc
Close #1

was not working for me. The above code would print only the first line of text in the file.
I am posting this question to show how I arrived at my solution via a text stream. I am hoping to save some of you looking for this info a little time. The following code has worked flawlessly for me. It has maintained the printer margins (of course I have only tried the default margin settings for a txt file in notepad), printed all the pages as they came -- all without me having to code an end page or new page sub.

dim fso as new FileSystemObject
dim tst as TextStream
dim strline as string
dim strFilePath as string

strFilePath = gstrFilePath

 If fso.FileExists(strFilePath) Then
            Set tst = fso.OpenTextFile(strFilePath, ForReading, False)
            Do Until tst.AtEndOfStream
                strline = strline & tst.ReadLine & Chr(13)
            Loop
            Printer.Print strline
            DoEvents
            Printer.EndDoc
            tst.Close
  End If


Hope this helps you guys.
0
Comment
Question by:jbaisden
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 25 total points
ID: 9743416
why not just read the entire file in first, then print it?

Open strFilePath For Input As #1
if not Eof(1)
  Printer.Print Input(LOF(1), 1)
wend
Printer.EndDoc
Close #1
0
 

Author Comment

by:jbaisden
ID: 9743580
I didn't think to do that. But I don't know if the character returns would be stored.

I did have a loop that went through a bunch of line reads and just added the new lines to the same string, but when I went to prnit that string only one line printed. I think I lost the formatting in that one as well.

I'm sure there are a ton of ways to do this. I just perused hundreds of threads and didn't find one that worked like I needed it to.
0
 
LVL 7

Expert Comment

by:_agj_
ID: 9745289
i think  bobbit31's mtd shud work. IF not use Get instead of Input and read in in binary mode.

what xactly do u need?
0
 

Author Comment

by:jbaisden
ID: 9747690
Well I'm not saying bobit31's code doesn't work. It is a slight variation of the various things I have tried to. However, I have a solution using the text stream object which seems to work just fine.

I don't have a question to ask on this thread. I just wanted to post the solution I found after hours of looking -- that sadly did not come from this site.
0

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

912 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

15 Experts available now in Live!

Get 1:1 Help Now