Solved

Load Text File Into Rich Text Box

Posted on 2001-06-12
5
201 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
Comment Utility

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
Comment Utility
    Me.RichTextBox1.LoadFile "c:\test.txt", rtfText
0
 
LVL 5

Expert Comment

by:aminerd
Comment Utility
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
Comment Utility
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
Comment Utility
Short and sweet, thats the way I like it, thanks!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now