Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-11-13
4
Medium Priority
?
1,074 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 75 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

885 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