Solved

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

Posted on 2003-11-13
4
1,065 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
Comment Utility
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
Comment Utility
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_
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 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

12 Experts available now in Live!

Get 1:1 Help Now