Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Display contents of text file in a listbox or textbox on Excel for

I have a macro that creates a text file (c:\temp.txt) with single entries per
line.
example: server1
server2
server3
server4

I would like to be able to retrieve the contents of this file into a listbox
or a textbox within an Excel VBA form. Seems simple enough but I cannot get
this to work.

Any suggestions?

Thanks in advance,

Scott


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default Display contents of text file in a listbox or textbox on Excel for

Hi,
- Add a reference to Microsoft Scripting Runtime library
- Set the textbox (say TExtbox1) 's Multiline property to True
Now the code:

Sub FillTextbox()
Dim fso As Scripting.FileSystemObject
Dim ts As TextStream
Dim filename As String
Dim fulltext As String

filename = "C:\credit.txt"
Set fso = New Scripting.FileSystemObject
Set ts = fso.OpenTextFile(filename, ForReading)

TextBox1 = ts.ReadAll

ts.Close
set ts=Nothing
set fso = nothing
End sub
--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"PcolaITGuy" wrote:

I have a macro that creates a text file (c:\temp.txt) with single entries per
line.
example: server1
server2
server3
server4

I would like to be able to retrieve the contents of this file into a listbox
or a textbox within an Excel VBA form. Seems simple enough but I cannot get
this to work.

Any suggestions?

Thanks in advance,

Scott


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Display contents of text file in a listbox or textbox on Excel for

You can use either of these to do what you asked (note that **no** scripting
libraries are required)...

Sub FillTextBox()
Dim FileNum As Long
Dim TotalFile As String
Dim PathFileName As String
PathFileName = "c:\temp.txt"
FileNum = FreeFile
Open PathFileName For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum
TextBox1.Text = TotalFile
End Sub

Sub FillListBox()
Dim X As Long
Dim FileNum As Long
Dim TotalFile As String
Dim PathFileName As String
Dim Records() As String
PathFileName = "c:\temp.txt"
FileNum = FreeFile
Open PathFileName For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum
Records = Split(TotalFile, vbNewLine)
For X = 0 To UBound(Records)
If Len(Records(X)) Then ListBox1.AddItem Records(X)
Next
End Sub

Rick


"PcolaITGuy" wrote in message
...
I have a macro that creates a text file (c:\temp.txt) with single entries
per
line.
example: server1
server2
server3
server4

I would like to be able to retrieve the contents of this file into a
listbox
or a textbox within an Excel VBA form. Seems simple enough but I cannot
get
this to work.

Any suggestions?

Thanks in advance,

Scott



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Display contents of a cell in a user form text box -- Excel 2003 VBA hiskilini Excel Discussion (Misc queries) 7 April 4th 23 10:22 AM
Display cell contents in a second worksheet file Jeff Weinberg Excel Discussion (Misc queries) 0 March 16th 07 05:48 PM
Display text box contents in cell msandomir Excel Discussion (Misc queries) 1 February 13th 07 02:18 PM
Display ALL text contents in cell Flipper1067 Excel Discussion (Misc queries) 10 July 27th 05 02:22 PM
Qn: Display Text in TextBox in Userform Michael Vaughan Excel Programming 1 August 18th 04 05:06 PM


All times are GMT +1. The time now is 05:10 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"