?
Solved

importing character from text file

Posted on 2003-02-22
5
Medium Priority
?
202 Views
Last Modified: 2010-04-07
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
0
Comment
Question by:hmahfouzy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 14

Accepted Solution

by:
aelatik earned 800 total points
ID: 7999320
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
Wend
MsgBox myVar
End Sub
0
 
LVL 18

Expert Comment

by:Sethi
ID: 7999329
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.


0
 

Expert Comment

by:miere
ID: 7999649
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
Myfile.SkipLine
' Read the 2nd Line
MyData = MyFile.ReadLine


Send me a Email if you want a Complete Help File about
File System Object:
miere00@hotmail.com
0
 
LVL 5

Expert Comment

by:Rhaedes
ID: 7999986
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,
Rhaedes

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
0
 
LVL 1

Author Comment

by:hmahfouzy
ID: 8002140
thanx this is what i really need this is the code
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question