Solved

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

Posted on 2003-11-13
4
1,070 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…

695 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