I need the simplest way possible to print an existing *.txt file
Any Help?
JO
Printable View
I need the simplest way possible to print an existing *.txt file
Any Help?
JO
You can do it two ways. By opening the text file and placing the contents into a variable and printing the variable, or using the ShellExecute API function (probably takes longer) to print it.
To do it through a variable:
Or you can do it by using the ShellExecute API function:Code:Private Sub Command1_Click()
Dim sText As String
Open "MyFile.txt" For Input As #1
sText = Input(LOF(1), 1)
Close #1
Printer.Print sText
Printer.EndDoc
End Sub
Code:Private Declare Function ShellExecute _
Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As _
Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Sub PrintFile(sFile As String)
ShellExecute Me.hwnd, "Print", sFile, _
vbNullString, vbNullString, 1 'or 0 for hidden
End Sub
Private Sub Command1_Click()
Call PrintFile("MyFile.txt")
End Sub
Could I use this one in Form_Load event then close the form after printing?
Private Sub Command1_Click()
Dim sText As String
Open "MyFile.txt" For Input As #1
sText = Input(LOF(1), 1)
Close #1
Printer.Print sText
Printer.EndDoc
End Sub
JO
'
Dim iFile As Integer
Dim cData As String
'
iFile = FreeFile
'
Open "C:\(YOURFILENAME).txt" For Input As #iFile
cData = Input(LOF(iFile), 1)
Close
'Debug.Print , cData
Printer.Print , cData
Printer.EndDoc
'
try this it whacks the whole file into a string and then prints the string, pretty quick
hope it helps
:) john:)
Quote:
Originally posted by joltremari
Could I use this one in Form_Load event then close the form after printing?
Private Sub Command1_Click()
Dim sText As String
Open "MyFile.txt" For Input As #1
sText = Input(LOF(1), 1)
Close #1
Printer.Print sText
Printer.EndDoc
End Sub
JO
Sure.
Code:Private Sub Form_Load()
Dim sText As String
Open "MyFile.txt" For Input As #1
sText = Input(LOF(1), 1)
Close #1
Printer.Print sText
Printer.EndDoc
DoEvents
Unload Me
Set Form1 = Nothing
End
End Sub
Thank you,
JO