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.
Printable View
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.
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
I tried:
I didn't get an error but I also didn't see any results.Code:Dim FSO as FileSystemObject
set FSO = new FileSystemObject
FSO.CopyFile "C:\test.txt", Printer.DeviceName
I tried:
and this works. Is there a cleaner way to do this print - a method that doesn't involve looping thru each line?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
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
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
I'm now using:
and it is working properly. Thank you!Code:Dim strTemp As String
Open "textFile.txt" For Input As #1
strTemp = Input(FileLen("textFile.txt"), 1)
Printer.Print strTemp
using printer object.
;)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
Here's anlother way...
Dim PrintIt
Dim PrintText As String
PrintText = "C:\textfile,txt"
PrintIt = Shell("c:\winnt\system32\notepad.exe /p " & PrintText, 6)
Cimperiali - thanks for the code. I'm using:
and I'm getting: Run-time error '55': File already open - on the Open FileName$ For Binary As #intFree line. Do you know why?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
Cubbie - what variable type is PrintIt? What should the code do?
Thanks for your help.
Tiff
I took out the first set of open and close code and it works:
Thank you!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
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?
I used a variant but a Long would probably be more appropriate
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...
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
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!!