|
-
March 29th, 2004, 10:49 AM
#1
how to print existing .txt file?
I have a txt file.
I know the path to the txt file on the local machine. (c:\textFile.txt)
How do I print this text file?
Thanks.
-
March 29th, 2004, 11:14 AM
#2
A trick: copyFile to printer
You can use Fso to copy file to printer, like in this article:
http://support.microsoft.com/default...b;en-us;252607
And you could also try copying a file to LPT1
(some other tricks with lpt1:
http://www.codeguru.com/forum/showth...ighlight=print )
Or: you read the file and print the variable
Last edited by Cimperiali; March 29th, 2004 at 11:21 AM.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
March 29th, 2004, 02:09 PM
#3
I tried:
Code:
Dim FSO as FileSystemObject
set FSO = new FileSystemObject
FSO.CopyFile "C:\test.txt", Printer.DeviceName
I didn't get an error but I also didn't see any results.
I tried:
Code:
Dim strTemp As String
Open "c:\text.txt" For Input As #1
Do Until EOF(1)
Line Input #1, strTemp
Printer.Print strTemp
Loop
and this works. Is there a cleaner way to do this print - a method that doesn't involve looping thru each line?
Secondly, is there a way to name the printed out file (as I'm printing to the pdfwriter)?
Thirdly, is there a way to set where the pdf printed file is saved to?
Thanks.
Tiff
-
March 30th, 2004, 05:50 AM
#4
about first: you can load all file in a var in a single step
(see Cakkie Faq-the sticy post), and print the var with a single
print instruction.
Do not know about second and third
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
March 31st, 2004, 12:18 PM
#5
another solution:
I'm now using:
Code:
Dim strTemp As String
Open "textFile.txt" For Input As #1
strTemp = Input(FileLen("textFile.txt"), 1)
Printer.Print strTemp
and it is working properly. Thank you!
-
April 1st, 2004, 08:10 AM
#6
-
April 1st, 2004, 11:10 AM
#7
Code:
Dim strTemp As String
Open "textFile.txt" For Input As #1
strTemp = getStringContentAtOnce("textFile.txt")
Printer.Print strTemp
Printer.EndDoc
'....
Public Function getStringContentAtOnce(ByVal FileName As String) As String
Dim intFree As Integer
' ensure that the file exists
If Len(Dir$(FileName)) = 0 Then
Err.Raise 53 ' File not found
End If
' open in binary mode
intFree = FreeFile
Open FileName$ For Binary As #intFree
' read the string and close the file
getStringContentAtOnce = Space$(LOF(intFree))
Get #intFree, , getStringContentAtOnce
Close #intFree
End Function
Last edited by Cimperiali; April 22nd, 2004 at 03:39 PM.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
April 1st, 2004, 12:24 PM
#8
Here's anlother way...
Dim PrintIt
Dim PrintText As String
PrintText = "C:\textfile,txt"
PrintIt = Shell("c:\winnt\system32\notepad.exe /p " & PrintText, 6)
-
April 5th, 2004, 03:51 PM
#9
Cimperiali - thanks for the code. I'm using:
Code:
Dim strTemp As String
Open "textFile.txt" For Input As #1
strTemp = getStringContentAtOnce("textFile.txt")
Printer.Print strTemp
Printer.EndDoc
Close #1
Public Function getStringContentAtOnce(ByVal FileName As String) As String
Dim intFree As Integer
' ensure that the file exists
If Len(Dir$(FileName)) = 0 Then
Err.Raise 53 ' File not found
End If
' open in binary mode
intFree = FreeFile
Open FileName$ For Binary As #intFree
' read the string and close the file
getStringContentAtOnce = Space$(LOF(intFree))
Get #intFree, , getStringContentAtOnce
Close #intFree
End Function
and I'm getting: Run-time error '55': File already open - on the Open FileName$ For Binary As #intFree line. Do you know why?
Cubbie - what variable type is PrintIt? What should the code do?
Thanks for your help.
Tiff
-
April 5th, 2004, 03:56 PM
#10
It works!
I took out the first set of open and close code and it works:
Code:
Dim strTemp As String
strTemp = getStringContentAtOnce("textFile.txt")
Printer.Print strTemp
Printer.EndDoc
Public Function getStringContentAtOnce(ByVal FileName As String) As String
Dim intFree As Integer
' ensure that the file exists
If Len(Dir$(FileName)) = 0 Then
Err.Raise 53 ' File not found
End If
' open in binary mode
intFree = FreeFile
Open FileName$ For Binary As #intFree
' read the string and close the file
getStringContentAtOnce = Space$(LOF(intFree))
Get #intFree, , getStringContentAtOnce
Close #intFree
End Function
Thank you!
-
April 6th, 2004, 09:52 AM
#11
run-time errors
For each solution that I've used below, sometimes I'll get errors. I'll use the same text file - sometimes it prints, sometimes it returns an error.
The most recent code worked fine yesterday.
Today (using the same file) I'm getting "Run-time error '482': Printer error" - for the line: Printer.Print strTemp
What's causing this error? Why is there sometimes an error and sometimes not?
-
April 6th, 2004, 10:35 AM
#12
I used a variant but a Long would probably be more appropriate
-
April 7th, 2004, 08:55 AM
#13
Might be some non printable chars?
Might it be in your variable you get some chars that are not accepted by printer?
For example, in my office there is a printer that usally does not accept pictures....But software does not crash - the Printer does crash...
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
April 7th, 2004, 11:35 PM
#14
Code:
'this will print a text file , it's ok and tested i have using in projects for fast printing
'for any problem pls inform
private sub Command1_Click()
PrintTheFile "c:\mytextfile.txt"
end sub
Public Sub PrintTheFile(FileToPrint As String)
Dim sTemp1 As String, sTemp2 As String, ii As Long
dim jj As Integer, i As Integer
Close #1
Close #2
Open FileToPrint For Binary As #1: Open Printer.Port For Binary As #2
ii = LOF(1) \ 8192
jj = LOF(1) Mod 8192
sTemp2 = String(8192, " ")
For i = 1 To ii
sTemp1 = sTemp2
Get #1, , sTemp1
Put #2, , sTemp1
Next
sTemp1 = String(jj, " ")
Get #1, , sTemp1
Put #2, , sTemp1
Close #1
Close #2
End Sub
Last edited by Cimperiali; April 8th, 2004 at 02:21 AM.
-
April 12th, 2004, 10:52 AM
#15
follow-ups and new questions
jkpradyumn:
Thanks for the code. I tried using it. I'm printing to Acrobat PDFWriter. The text file is sent to the correct printer and is queued. Then the printer status changes to Error-Printing. Is there a way to modify your code so that it will work with Acrobat PDFWriter?
*****
Cimperiali:
The text file only has alpha characters plus some keyboard symbols. Processing the same file - sometimes it works, sometimes it doesn't...
*****
Since the file it being printed to Acrobat PDFWriter a new file is being created. Is there a way set the default folder the file is saved to? I tried using commonDialog1.InitDir = "C:\" but that didn't do anything.
Thank you for your time!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|