?
Solved

Load Text File Into Rich Text Box

Posted on 2001-06-12
5
Medium Priority
?
230 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
[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 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 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

801 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