Click to See Complete Forum and Search --> : text file opening


DSL
April 5th, 2000, 05:32 PM
hello, i made a text file program like notepad where you save and open text files, but when i open a text file with spaces it only opens one line of it, heres the code for opening it, tell me whats wrong:

Private Sub mnuopen_Click()
On Error GoTo h
Dim strTemp As String
dlg.Filter = "Text files (*.txt)|*.txt"

dlg.FileName = "*.txt"
dlg.ShowOpen
If Dir(dlg.FileName) > "" Then

Open dlg.FileName For Input As #1

Line Input #1, a
text1 = a

Close #1
Else
MsgBox "File not found", vbExclamation, "Error" & Err.Description
End If
h:

End Sub

d.paulson
April 5th, 2000, 10:31 PM
Well, It looks as if you only read one line. You would have to add the following code to read the rest.

Open dlg.FileName for input as #1

while not eof(1)
Line input #1, a
text1 = text1 & a
wend

Close #1





David Paulson

d.paulson
April 6th, 2000, 01:26 PM
You may also have to add a cr/lf
text1 = text1 & vbcrlf

David Paulson

Johnny101
April 6th, 2000, 01:50 PM
Your code only reads one line of the source file - you could do what the above post suggests. I personally have become very partial to the object way of using text files - the FileSystemObject:

'set a reference to the Microsoft Scripting Runtime

private Sub mnuopen_Click()
Dim FSO as Scripting.FileSystemObject
Dim Filer as Scripting.TextStream
Dim sFile as string
Dim strTemp as string

on error GoTo h
dlg.Filter = "Text files (*.txt)|*.txt"

dlg.FileName = "*.txt"
dlg.ShowOpen

If Dir(dlg.FileName) > "" then
sFile = dlg.FileName
set FSO = new Scripting.FileSystemObject
set Filer = FSO.OpenTextFile(sFile, ForWriting, false)

text1.Text = filer.readall

Filer.Close
else
MsgBox "File not found."
End If

set Filer = nothing
set FSO = nothing
End Sub




Hope that helps,

John

John Pirkey
MCSD
http://www.ShallowWaterSystems.com
http://www.stlvbug.org

April 16th, 2000, 09:16 PM
Add a loop to scan from the begin to the end of file :

open ....

while not eof (1)
line input #1, a$
text1 = text1.text & a$
loop

close 1