Solved

Load Text File Into Rich Text Box

Posted on 2001-06-12
5
209 Views
Last Modified: 2010-05-02
How would I go about loading all the text from a file, like test.txt into a rich text box on a form by clicking a command button? Thanks in advance!
0
Comment
Question by:cybrthug
5 Comments
 
LVL 4

Expert Comment

by:beckingh
ID: 6182366

This code will read a text file into a regular text box


Dim TextLine as string

Open "c:\list.ini" For Input As #1 ' first open file

Do Until EOF(1)
     Line Input #1, TextLine
     Text1.Text = Text1.Text & vbCrLf & TextLine
Loop
     
Close #1



0
 
LVL 15

Accepted Solution

by:
ameba earned 50 total points
ID: 6182381
    Me.RichTextBox1.LoadFile "c:\test.txt", rtfText
0
 
LVL 5

Expert Comment

by:aminerd
ID: 6182482
Ameba's way is easiest, however, if you want to read the file yourself, I suggest something like the following:

Public Function OpenFile(FileName As String) As String
 
  Dim intFile As Integer
  Dim strTemp As String
 
  'Get a new file number
  intFile = FreeFile
 
  'Read the contents of the file into strTemp
  Open FileName For Input As intFile
    strTemp = Space(LOF(intFile))
    Get intFile, 0, strTemp
  Close intFile
 
  'Return the file contents
  OpenFile = strTemp
 
End Function

If you hardcode a file number (for example, #1), you could potentially run into problems. It's always better to use the FreeFile function. Also, by reading the entire file in at once you save time (and skip a loop).

You could use the function I posted like:

RichTextBox.Text = OpenFile("c:\test.txt")
0
 

Expert Comment

by:gauravdhup
ID: 6182592
here is the code

Open App.Path & "\test.txt" For Input As #1
Do While Not EOF(1)   ' Check for end of file.
  linesfromfile = StrConv(InputB(LOF(1), 1), vbUnicode)
  Text26.Text = linesfromfile
Loop
Close #1


here text26.text is the richtextbox
0
 

Author Comment

by:cybrthug
ID: 6183005
Short and sweet, thats the way I like it, thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

778 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