Can anyone please tell me if its possible to read a whole listbox into a text file
Thanks
Printable View
Can anyone please tell me if its possible to read a whole listbox into a text file
Thanks
I suppose that you could read each listitem one at a time and write them to a file...
don't know if that is what you are looking for
H.
Code:Option Explicit
Private Sub Command1_Click()
'fill a listbox with junk
Dim i As Integer
For i = 1 To 10
List1.AddItem CInt((Rnd * 1022)) + 1
Next i
'open a text file for output (overwrite)
Open "C:\my documents\myfile.txt" For Output As #1
'loop the list and print it's contents to the file
For i = 0 To List1.ListCount - 1
List1.ListIndex = i
Print #1, List1.Text
Next i
Close i
'reset items
List1.ListIndex = 0
Command1.Enabled = False
End Sub
To load or save to file/listbox:
Code:Private Sub List_Load(TheList As ListBox, FileName As String)
On Error Resume Next
Dim TheContents As String
Dim fFile As Integer
fFile = FreeFile
Open FileName For Input As fFile
Do
Line Input #fFile, TheContents$
TheList.AddItem TheContents$
Loop Until EOF(fFile)
Close fFile
End Sub
Call List_Load(List1, "C:\MyFile.txt")
Private Sub List_Save(TheList As ListBox, FileName As String)
On Error Resume Next
Dim Save As Long
Dim fFile As Integer
fFile = FreeFile
Open FileName For Output As fFile
For Save = 0 To TheList.ListCount - 1
Print #fFile, TheList.List(Save)
Next Save
Close fFile
End Sub
Call List_Save(List1, "C:\MyFile.txt")