Hi all. could any one show me how i can save all contents of a listbox to a text file ? Thanks
Printable View
Hi all. could any one show me how i can save all contents of a listbox to a text file ? Thanks
1. Do you know how to create & open a text file?
2. Do you know how to loop through a listbox?
Show us what you have so far.
He probably doesn't have anything because he doesn't know how. We could at least mention the process, which you touched on.
1. Open textfile for writing. (Open statement)
2.Loop through listbox items with a For Next loop
3.Save each list item to the file inside the loop (Print statement)
4.Close the file (Close statement)
Look into these, search the forums, (including code bank) and post back if you run into issues.
Uses common dialog to load and save listboxesCode:Dim cdlg As Object
Public Sub xSaveList(List As ListBox, Optional clear As Boolean = False)
'self explanatory
On Error Resume Next
Dim lngSave As Long
Set cdlg = CreateObject("MSComDlg.CommonDialog")
cdlg.Filter = "Text Files (.txt)|*.txt"
cdlg.filename = ""
cdlg.ShowSave
cdlg.DialogTitle = "Save List"
If LenB(cdlg.filename) = 0 Then Exit Sub
Dim filename As String
filename = cdlg.filename
If clear Then List.clear
Open filename$ For Output As #1
For lngSave& = 0 To List.ListCount - 1
Print #1, List.List(lngSave&)
Next lngSave&
Close #1
End Sub
Public Sub xLoadList(List1 As ListBox, Optional clear As Boolean = True)
'self explanatory
Set cdlg = CreateObject("MSComDlg.CommonDialog")
cdlg.Filter = "Text Files (.txt)|*.txt"
cdlg.filename = ""
cdlg.DialogTitle = "Load List"
cdlg.ShowOpen
If LenB(cdlg.filename) = 0 Then Exit Sub
Dim filename As String
filename = cdlg.filename
Dim lstInput$, oneinput, twoinput As String
If clear Then List1.clear
On Error Resume Next
Open filename$ For Input As #1
While Not EOF(1)
Input #1, lstInput$
List1.AddItem lstInput$
Wend
Close #1
End Sub
Thanks all . I managed to loop the listbox now how i write it to a text file ?
2 Code:
Private Sub Command8_Click() Dim I As Long For I = 0 To List1.ListCount - 1 'If List1.Selected(I) = True Then 'MsgBox List1.Text MsgBox List1.List(I) 'End If Next End Sub
Zach thanks for your code but can you tell me what controles and buttons i need to run it ? i want to learn this step by step.
Here is how to both save the listbox, and reload it again.Code:Private Sub SaveLoadListbox(plstLB As ListBox, _
pstrFileName As String, _
pstrSaveOrLoad As String)
Dim strListItems As String
Dim i As Long
Select Case pstrSaveOrLoad
Case "save"
Open pstrFileName For Output As #1
For i = 0 To plstLB.ListCount - 1
plstLB.Selected(i) = True
Print #1, plstLB.List(plstLB.ListIndex)
Next
Close #1
Case "load"
plstLB.Clear
Open pstrFileName For Input As #1
While Not EOF(1)
Line Input #1, strListItems
plstLB.AddItem strListItems
Wend
Close #1
End Select
End Sub
Private Sub Form_Load()
Call SaveLoadListbox(List1, "c:\Listbox.txt", "load")
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call SaveLoadListbox(List1, "c:\Listbox.txt", "save")
End Sub
hack. this code is perfectooooo
Build a form with a list box and a command button. Then apply this code:Quote:
Originally Posted by tony007
Note: that will rebuild the text file each time, so you may wish to open it for Append.Code:Private Sub Command1_Click()
' Save List Box Items in File
Open "MyTextFile.txt" For Output As #1
For I = 0 To List1.ListCount - 1
Print #1, List1.List(I)
Next
Close
End Sub
Private Sub Form_Load()
'Add some data to list box
For I = 1 To 10
List1.AddItem Str$(I)
Next
End Sub
Hello ...I loaded a txt file 1204 kb with hundreds/thousands of rows into a listbox.
I added something to the listbox (15 lines) successfully but when I saved it (overwriting the orig txt file). The txt file becomes blank.
I used this code:
Dim AA As Integer,b as string ,v As Integer
b=data.txt
AA = FreeFile()
Open b For Output As AA 'To rewite the file replace "Append" with "Output"
For v = 0 To List3.ListCount - 1
Print #AA, List3.List(v)
Next
Close #AA
I tried using the same code above with other txt files and it worked perfectly.
Is the problem because the file I tried to overwrite is too large that it went from 1204kb to 0kb (blank file)?
b = data.txt
Is this exactly how you are initializing b or is it actually b = "data.txt"
If b = "data.txt' and not b = data.txt then when you first open the file as Output it is set to 0 bytes. So, if you wind up with 0 bytes after the loop has finished then the only thing I see is the listbox has 0 entries in it otherwise your code looks OK
Maybe you should show the code you use to load the list3
.I loaded a txt file 1204 kb with hundreds/thousands of rows into a listbox.
Don't quite understand your statement here. You say "I loaded a txt file...." What do you mean by loaded? Are you saying you loaded a text file and then from the text file you loaded it into a listbox?
BTW: Next time you have a question please open a new thread and put a reference in that thread to the old thread instead of replying to a thread from Nov '08 which is 4 years old
The listbox has a limit of 32767 items.
Not sure if this is the reason why your file did not save (error), but your code looks fine.
> You realize, of course, that you opened a thread that was five years old? Where have you been and what have you been doing for the last five years?
They've probably been waiting for that thousands-of-items list to load! ;-)
This line, b=data.txt, doesn't make sense to me.
If data.txt is the text file to which you are attempting to write the contents of list3, then b should equal something like this:
app.path & "\data.txt" or
if you prefer, "C:\documents and settings\user\yourcomputerusername\mydirectory\data.txt" Or something similar where both the path and text file are in quotes.
And, what Max says can put a kabosh on your code.....limited number of entries in a listbox.