Make sure you add a Button (Command1) and a TextBox (Text1) to the project. Set MultiLine = True under TextBox properties. Also, place a file with few lines named myfile.txt under C:\.
Code: Select all
Option Explicit
Dim FileNum As Integer, strText As String, strLine As String, LineCount As Long
Private Sub Command1_Click()
' Get a free file number
FileNum = FreeFile
' Open a text file for input. inputbox returns the path to read the file
Open "C:\myfile.txt" For Input As FileNum
LineCount = 1
' Read the contents of the file
Do While Not EOF(FileNum)
' Read line
Line Input #FileNum, strLine
' Add line text to main text with line breaks
strText = strText & strLine & vbCrLf
LineCount = LineCount + 1
Loop
Text1.Text = strText
' Close the file
Close FileNum
End Sub