How would i go about calling this function
VB Code:
Private Function SaveFileFromTB(TxtBox As Object, _ FilePath As String, Optional Append As Boolean = False) _ As Boolean
Jenova
Printable View
How would i go about calling this function
VB Code:
Private Function SaveFileFromTB(TxtBox As Object, _ FilePath As String, Optional Append As Boolean = False) _ As Boolean
Jenova
If SaveFileFromTB(Text1,"c:\mypath") = True Then
' do something
Else
' do something else
End If
Dim ReturnValue as boolean
ReturnValue = SaveFileFromTB(textbox, "c:\Path")
VB Code:
If SaveFileFromTB(Text1, MyPathString) Then ' do something End if ' or If SaveFileFromTB(Text1, MyPathString, True) Then ' do something End if
Sorry, should have explianed better. This is the function:
VB Code:
Private Function OpenFileToTB(TxtBox As Object, _ FilePath As String, Optional Append As Boolean = False) _ As Boolean Dim iFile As Integer Dim s As String If Dir(FilePath) = "" Then Exit Function On Error GoTo ErrorHandler: s = TxtBox.Text iFile = FreeFile Open FilePath For Input As #iFile s = Input(LOF(iFile), #iFile) If Append Then TxtBox.Text = TxtBox.Text & s Else TxtBox.Text = s End If LoadFileToTB = True ErrorHandler: If iFile > 0 Then Close #iFile End Function
I need to call it from the form's load event. Or does anyone have an easier way to open a htm/txt file to a text box.
It is useless to call that function from the form's load event since it uses the contents of a textbox and that textbox can't at that point have anything in it.
I am tring to open a htm file so it's practically source when viewed in my program. I am using this code to do it
VB Code:
Private Sub Form_Load() On Error Resume Next Dim handle As Integer Dim file As String handle = FreeFile Open "C:\Documents and Settings\Carl\Desktop\index.htm" For Input As handle Do While Not EOF(handle) Text1.TextRTF = file Loop Close #handle End Sub
Slight drawback, it crashes when i run the program. Any ideas on how i can make this work
It doesn't "crash", you have an infinite loop. EOF(handle) will never be true because you are not moving through the file.Quote:
Slight drawback, it crashes when i run the program.
You need to retrieve the file contents using Input or Line Input statements.
The first thing you should do is to remove the On Error Resume Next line.
Apologies.
On error resume next was to see if the that would solve it but i't didn't. Thanks for the advice on an infinite loop but if i use Line Input or Input then it will give me the last line of the HTML file which is </html>.
Jenova
That's because in the code you posted you aren't appending the data so you just see the last line.
lol sorted.
VB Code:
Private Sub Form_Load() Dim handle As Integer Dim file As String handle = FreeFile Open "C:\Documents and Settings\Carl\Desktop\index.htm" For Input As handle Do While Not EOF(handle) file = Input(LOF(handle), #handle) Text1.TextRTF = file Loop Close #handle End Sub
Thanks guys
You can now help us by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button which will let everyone know that you have your answer.