ListBox Value to Text File

skaswani

New member
Local time
Today, 05:50
Joined
Apr 7, 2010
Messages
5
Dear All,

i need your kind help to create a text file from list box values
list box may have one or more then one values

Excel 2002
VBA


:confused:
 
Hi and welcome to the forum. How much help are you going to need? For instance: Have you ever created a file in code before? Do you know how to use a For...Each...Next loop in VBA?
Cheers,
 
Thanks for Welcome :)

what i did
i have a user form, one list box and button

my code


Private Sub CommandButton1_Click()
Dim mytext As String
'mytext = text1.Text
Open "c:\myfile.txt" For Append As #1
For i = 1 To ListBox1.ListCount
Write #1, ListBox1.Text
Next
Close #1
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize()
ListBox1.AddItem "Jan"
ListBox1.AddItem "Feb"
ListBox1.AddItem "March"
End Sub

as you can see i am facing 2 problems
1) i am using append keyword which i dont want, what i want it always create a new file
2) export all values of list item to text file, while i am looping without index :(

i dont know more abt VBA, but learing :)
please help me

Thanks and Regards,
 
Here's the approach I would take, using a File System Object...
Code:
Sub CreateNewTextFileExampleUsingFSO()
   Const FOR_READING = 1
   Const FOR_WRITING = 2
   Const FOR_APPENDING = 8
   
   Dim fso As Object
   Dim ts As Object
   
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set ts = fso.OpenTextFile("c:\AA_Testing.txt", FOR_WRITING, True)
   
   For i = 1 To 12
      ts.Write i & ", "
      ts.WriteLine MonthName(i)
   Next

End Sub
To early bind, set a reference to Microsoft Scripting Runtime. This object model provides very rich functionality for working with files.
If you have more questions feel free to post ....
Cheers,
 
THank you so much

i have made few changes as per my need and its working:)

Sub WriteToATextFile()
Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8

Dim fso As Object
Dim ts As Object

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile("c:\AA_Testing.txt", FOR_WRITING, True)

For i = 0 To ListBox1.ListCount - 1
'ts.Write i & ", "
ts.WriteLine ListBox1.Column(0, i)
Next
End Sub



THanks alot dear
 
No. You've posted back to say you've solved the problem, and that's sufficient. Glad it worked out for you.
Cheers,
 

Users who are viewing this thread

Back
Top Bottom