Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I print a text file to LPT1 with VB6?.

Status
Not open for further replies.

Hulisi

Programmer
Jan 28, 2001
31
TR
Hi
How can I print a text file to LPT1 with VB6?.
Please help me.

 
Are you asking how to read the file and print it? Or do you just want to know how to send text to the printer.

Take a look at VB’s Printer object and its Print method

Printer.Print "Text To Print"
 
To access the printer port, one must first install
the free .dll by Prof. Anjan, a college professor in
India. It's calleed, vbio.dll.
To find his page, do a google.com search!
 

Assuming that you have a printer driver set up on your machine you can check its port by Printer.Port. This checks the default printer of the machine that the program is running on. If this then equals what you want(in debug ?Printer.Port) then you can use one of my other suggestions in the other thread that you posted thread222-397861. If you need to change the printer that you print to search this forum for "SetDefaultPrinter API" using all words.

However if you do not want to set the default printer to another printer but you do want to use the printer object to print your text file to the printer then you could use some code like this to find a printer driver that is set to LPT1:...

[tt]
Dim P As Printer

For Each P In Printers
If P.Port = "LPT1:" Then
Set Printer = P
Debug.Print Printer.DeviceName
Exit For
End If
Next

[/tt]

This loops through all of the printer drivers installed on the system looking for a printer whose port is "LPT1:" and sets the applications printer object to that driver (added a debug to find its name in design).

So let us assume that via the code above you have found a printer that is connected to LPT1:. You could then use the richtextbox to print your text file like so...

[tt]
RichTextBox1.SelStart = 0
RichTextBox1.SelPrint Printer.hDC
[/tt]

But, you really do not need to do that considering that you have set the application to what printer you want to use. You could use a regular text box to accomplish what you want also...

[tt]
Printer.Print Text1.Text
Printer.EndDoc
[/tt]

And, for the sake of arguement, lets say you do not want to display/load the text from a file into a textbox/richtextbox you could then use...

[tt]
Dim FNumb As Integer, FName As String, TextLine As String

FName = "Your path to text file"
FNumb = FreeFile

Open FName For Input As #FNumb

Do While Not EOF(FNumb)
Line Input #FNumb, TextLine
Printer.Print TextLine
Loop

Close #FNumb
Printer.EndDoc
[/tt]

but of course the code above would not take into consideration the length of the line of text and you could lose some data on the output, and if that is a concern then I would suggest that you use the richtextbox for your output as shown above.

I really hope that helps. Good Luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top