Consulting

Results 1 to 2 of 2

Thread: Solved: Excel VBA - Linkg a TextBox to a TXT File

  1. #1
    VBAX Regular
    Joined
    Oct 2010
    Posts
    26
    Location

    Question Solved: Excel VBA - Linkg a TextBox to a TXT File

    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

    [VBA]
    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
    [/VBA]

  2. #2
    VBAX Regular
    Joined
    Oct 2010
    Posts
    26
    Location
    nevermind after a long google search found something that works, unless anyone got anthing better.

    [vba]
    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

    [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •