Load File To List Box

The code below reads a single line from a file each time, and add it do a List Box

Preparations

Add 1 List Box and 1 Command Button to your form.

Form Code

Private Sub Command1_Click()
    Dim File, NextLine As String
    ' clear the List Box
   
List1.Clear
    ' replace the "c:\autoexec.bat" below with the name of the file
    ' you want to load to the list box
   
File = "c:\autoexec.bat"
    ' the FreeFile function assign unique number to the Filenum variable,
    ' to avoid collision with other opened file
   
Filenum = FreeFile
    Open File For Input As Filenum
    ' do until the file reach to its end
   
Do Until EOF(Filenum)
    ' read one line from the file to the NextLine String
       
Line Input #Filenum, NextLine
    ' add the line to the List Box
       
List1.AddItem NextLine
    Loop
    ' Close the file
    Close
End Sub


Go Back