|
-
Mar 28th, 2007, 09:09 PM
#1
Thread Starter
Hyperactive Member
[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
Please Help Us To Save Ana
-If you have been Helped, Please Mark Thread [Resolved] using Thread Tools
-If someone has helped you, Please rate their post.
-
Mar 28th, 2007, 09:22 PM
#2
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
-
Mar 28th, 2007, 09:24 PM
#3
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 :/
Last edited by kows; Mar 28th, 2007 at 09:27 PM.
-
Mar 28th, 2007, 09:27 PM
#4
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.
-
Mar 28th, 2007, 09:32 PM
#5
Re: Save file without using save dialog?
 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.
-
Mar 28th, 2007, 09:38 PM
#6
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|