Hi All,
I have an excel macro which includes a form with two listboxes.
I wish to load the contents of a 1.txt in the ListBox1 and the contents of file 2.txt into the ListBox2.
I have been using the code as given below for the purpose... which works on one machine but seems to be giving some problems in another machine. Both the machines have the same version of Excel... maybe something to do with the issue of VB Runtime File upgrades...?
Can someone tell me of some good code which is stable enough to work on all versions of Excel.. irrespective of the VB files in the machine!
thanks,
Lonely
VB Code:
Dim IsFileThere As String Dim tempString As String gFileName = "C:\Folder1\1.txt" gFileName2 = "C:\Folder1\2.txt" ' first, see if file exists, and prevent error if it doesn't ' Dir is a VB function that returns the full path to a file ' If file doesn't exist, Dir returns a blank, ie. "" IsFileThere = Dir(gFileName) If IsFileThere = "" Then ' just exit this sub, don't cause trouble Exit Sub End If ' now clear the list, or the list will keep growing ' it willl Append the txt file to the existing list listbox1.Clear ' OK, since the file does exist, read it in Open gFileName For Input As 1 While Not EOF(1) ' tempString just holds one line at a time Line Input #1, tempString listbox1.AddItem RTrim(tempString) Wend Close 1 Open gFileName2 For Input As 1 While Not EOF(1) ' note that tempString just holds one line at a time Line Input #1, tempString ListBox2.AddItem RTrim(tempString) Wend Close 1




Reply With Quote