• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

importing character from text file

hi all i would like to know how can i import all lines in a text file with out opening note pad or so as it will be in the backgroud of the main form

in case i would like to brouse some folders that contailn one text file at a folder to le the form able to import all character  in a text list as it will be backgrpund prossess

thanx all
1 Solution
This is a way to read a textfile line by line, myVar contains the entire textfile....

Private Sub Form_Load()
Dim NewLine, myVar As String
Open "c:\boot.ini" For Input As #1
While Not EOF(1)
Line Input #1, NewLine
myVar = myVar & NewLine & vbCrLf
MsgBox myVar
End Sub
You will have to use FileSytemObjects in VB for this. Set References to Microsoft Scripting Runtime and write the following code:

Dim objFSO as FileSystemObject
Dim objTextFile As TextStream
Dim strFileData as string

Set objTextFile = objFSO.OpenTextFile(strFileName, ForReading)
'strFilename should be the name of the file along with its exact path.

strFileData = objTextFile.ReadAll

Simple. It will not open any file but all the lines of the file.

If you want read a file and put some data at a textbox or at another thing, i think U must now the FILE SYSTEM OBJECT:

Here there's a little guide:

' Principal Command to FSO
dim fso
fso = CreateObject("Scripting.FileSystemObject")

You may use the following Function to Open a File:

Function readFile(File As String) As String
' Principal Command to FSO
dim fso, MyFile
fso = CreateObject("Scripting.FileSystemObject")
MyFile = fso.OpenTextFile(File, 2, True)
' Read the file
readFile = Myfile.ReadAll
End Function

TextBox1.text = readFile("c:\testFile.txt")

You can use the method READLINE to read a line,
and you can use the method SKIPLINE to skip a line.

' Principal Command to FSO
dim fso, MyFile, MyData
fso = CreateObject("Scripting.FileSystemObject")
MyFile = fso.OpenTextFile(File, 2, True)
' Skip to the 2nd line
' Read the 2nd Line
MyData = MyFile.ReadLine

Send me a Email if you want a Complete Help File about
File System Object:
Re-reading your question, do you mean that you simply want to be able to 'browse' your text files, and have their contents appear in a window? When you say 'all lines' do you just mean 'the whole thing'?
If this is not what you mean, then ignore me. If it is what you mean, then put a DirListBox (called Dir1), a FileListBox (File1) and a webbrowser (WebBrowser1) on a form and add the code below.
Just in case, it will also let you display htm files, jpg, gif, doc, pdf, and anything else that a browser can show.

Kindest regards,

Dim fileType As String
Dim acceptedTypes As String

Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub

Private Sub File1_Click()
fileType = "." & LCase(Mid$(File1.FileName, InStrRev("." & File1.FileName, "."))) & "."
'Leave the sub if the filetype is not accepted
If InStr(1, acceptedTypes, fileType) = 0 Then
WebBrowser1.Navigate2 ("about:blank")
Exit Sub
End If
'Filetype is good, so send the browser to the file
WebBrowser1.Navigate2 (Replace(File1.Path & "\" & File1.FileName, "\\", "\"))
End Sub

Private Sub Form_Load()
'acceptedTypes contians a list of filetypes
'to be shown in the browser
acceptedTypes = ".txt.htm.html.jpg.jpeg.gif.doc.pdf."
WebBrowser1.Navigate2 ("about:blank")
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
'Make sure txt files are properly wrapped
WebBrowser1.Document.body.Style.WordWrap = "break-word"
End Sub
hmahfouzyAuthor Commented:
thanx this is what i really need this is the code

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now