Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read multiline text file

Status
Not open for further replies.

kalle82

Technical User
Apr 2, 2009
163
SE
Hi!

Im using excel 2003. I do NOT have acess to the rich textbox ;(, and I can't add it to our system because of security measures.

So I use a textbox to get save data to textfiles, works nice!

The data in the textbox often contains multiple rows.

Example:

Hi! We are informing you of XXXXXXX.

In order to gain that bonus you will need to.

- DO

- DO

As you can see there are many lines that are empty.

So my question is.. How do I make the program add a "vbcrlf", when it sees an empty row..? I use the textbox to save templates, that I later send over to word..

I use this code to read everything into my textbox.

Code:
Dim fpath As String

fpath = Workbooks(1).Path & "\handläggningsstöd\mallar\" & sokvag

If FileOrDirExists(fpath) Then

Dim oFSO As New FileSystemObject
Dim oFS


Set oFS = oFSO.OpenTextFile(fpath)

Do Until oFS.AtEndOfStream

*******First i did this*** Works but i dont get any empty lines
Stext = Stext & oFS.ReadLine
Loop

****then i tried this**** This method does not give me any result..?

If oFS.ReadLine = "" Then
Stext = Stext & vbCrLf
Else
Stext = Stext & oFS.ReadLine
End If




 
You can read/write whole file/textbox:
Code:
Private Sub cmdRead_Click()
Set oFS = oFSO.OpenTextFile(fpath, ForReading, True)
Me.TextBox1.Text = oFS.ReadAll
End Sub

Private Sub cmdWrite_Click()
Set oFS = oFSO.OpenTextFile(fpath, ForWriting, True)
oFS.Write Me.TextBox1.Text
End Sub

combo
 
Either:
Code:
Set oFS = oFSO.OpenTextFile(fpath)
Do Until oFS.AtEndOfStream
  Stext = Stext & oFS.ReadLine & vbCrLf
Loop
or:
Code:
Set oFS = oFSO.OpenTextFile(fpath)
Stext = oFS.ReadAll

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Another way:
Code:
Dim strTextLine As String

Open fpath For Input As #1
Do While Not EOF(1)             [green]' Loop until end of file.[/green]
   Line Input #1, strTextLine   [green]' Read line into variable.[/green]
   Stext = Stext & strTextLine [blue]& vbCrLf[/blue]
Loop
Close #1
You may or may not need the [blue]BLUE[/blue] piece of code.

Have fun.

---- Andy
 
Ahh thanks guys :)

Cool I ended up doing the: Stext = Stext & strTextLine & vbCrLf
solution...

Really good to get to know the readall thing to!

BIG ups! To PHV, combo, Andrzejek!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top