PC Review


Reply
Thread Tools Rate Thread

Display contents of text file in a listbox or textbox on Excel for

 
 
PcolaITGuy
Guest
Posts: n/a
 
      13th Dec 2007
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 With Quote
 
 
 
 
sebastienm
Guest
Posts: n/a
 
      13th Dec 2007
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
>
>

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      13th Dec 2007
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" <(E-Mail Removed)> wrote in message
news:423B4F7E-6390-49A5-93EF-(E-Mail Removed)...
>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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Listbox won't display contents until I click on it SteveL5231 Microsoft Access Forms 4 26th Mar 2010 03:32 AM
Re: click event to transfer contents of textbox to text file - is this possible? Allen Browne Microsoft Access 0 5th Mar 2009 01:50 AM
Display contents of a cell in a user form text box -- Excel 2003 VBA hiskilini Microsoft Excel Misc 5 19th Feb 2007 03:05 PM
Excel 2002 Display Directory contents Listbox mulr1966 Microsoft Excel Misc 1 7th Apr 2004 02:03 AM
Display contents of text file in the Browser Rathtap Microsoft ASP .NET 3 26th Mar 2004 02:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:24 PM.