Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Load Text File Into Rich Text Box

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
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 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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

636 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