Solved

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

Posted on 2003-11-13
4
1,067 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 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

10 Experts available now in Live!

Get 1:1 Help Now