[RESOLVED] Save file without using save dialog?
Im using this to load items into a listbox without open dialog how would i save the updated list to the same file, using the same kind of code?
Code:
Private Sub cmdLoad_Click()
Dim fnum As Integer
Dim file_line As String
fnum = FreeFile
Open txtFile.Text For Input As fnum
Do While Not EOF(fnum)
Line Input #fnum, file_line
lstLines.AddItem file_line
Loop
End Sub
Re: Save file without using save dialog?
vb Code:
Private Sub cmdSave_Click()
Dim fnum As Integer
Dim intLoop As Integer
fnum = FreeFile
Open txtFile.Text For Append As #fnum
For intLoop = 0 To lstLines.ListCount - 1
Print #fnum, lstLines.List(intLoop)
Next intLoop
Close #fnum
End Sub
Re: Save file without using save dialog?
vb Code:
Dim fnum as Integer, i as Integer, entire_list as String
'loop through list adding a line break (vbCrLf) after each value, saving it all to 'entire_list'
For i = 0 To lstLines.ListCount - 1
entire_list = entire_list & vbCrLf & lstLines.List(i)
Next i
'write the text file
fnum = FreeFile
Open txtFile.Text For Output as fnum
Print #fnum, entire_list
Close fnum
edit: digirev, I don't believe he wants to append to the file, seeing as how it's an updated list made up of that file.
edit2: added a bit more code to my example :/
Re: Save file without using save dialog?
You need to Open ... For Output to overwrite the original with the updated list. Open ... For Append adds to the existing file.
Re: Save file without using save dialog?
Quote:
Originally Posted by kows
edit: digirev, I don't believe he wants to append to the file, seeing as how it's an updated list made up of that file.
Oops, didn't catch that. ;)
Re: [RESOLVED] Save file without using save dialog?
If speed is an issue, this might be the fastest method.
vb Code:
Dim fnum As Integer
Dim i As Long
Dim sLines() As String
' Populate the listbox
fnum = FreeFile
Open txtFile.Text For Input As #fnum
sLines() = Split(Input(LOF(fnum), fnum), vbCrLf)
Close #fnum
lstLines.Clear
For i = 0 To UBound(sLines)
lstLines.AddItem sLines(i)
Next i
' Save the list
ReDim sLines(lstLines.ListCount - 1)
For i = 0 To UBound(sLines)
sLines(i) = lstLines.List(i)
Next i
fnum = FreeFile
Open txtFile.Text For Output As #fnum
Print #fnum, Join(sLines, vbCrLf);
Close #fnum