PDA

View Full Version : Solved: Excel VBA - Linkg a TextBox to a TXT File



gunny2k9
06-01-2011, 05:33 AM
ok so i have a userform with a TextBox and would like the textbox to display text from a text file (outside of excel)

basicly a 3rd party program writes an error log and then using Excel vba the apps UserForm displays said error log in a textbox on said UserForm ( to save the user from looking for the error log file

?

i know this code can open and read a text file to place said text to the debug screen


Sub ReadStrangeFile()
' Requires a reference to Microsoft Scripting Runtime (Tools > References)
Dim FSO As FileSystemObject
Dim FSOFile As File
Dim FSOStream As TextStream
Set FSO = New FileSystemObject
Set FSOFile = FSO.GetFile("C:\Sample.txt")
Set FSOStream = FSOFile.OpenAsTextStream(ForReading, TristateUseDefault)
Do While Not FSOStream.AtEndOfStream
Debug.Print FSOStream.ReadLine
Loop
End Sub

gunny2k9
06-01-2011, 05:55 AM
nevermind after a long google search found something that works, unless anyone got anthing better.


Private Sub Button1_Click()
Application.ScreenUpdating = False
Dim sTxt$, sText$, sPath$
sPath = "C:\sample.txt"
If Dir(sPath) = "" Then
MsgBox "File was not found."
Exit Sub
End If
Close
Open sPath For Input As #1
Do Until EOF(1)
Line Input #1, sTxt
sText = sText & sTxt & vbLf
Loop
Close
sText = Left(sText, Len(sText) - 1)
TextBox1.Text = sText
Application.ScreenUpdating = True
End Sub