Consulting

Results 1 to 3 of 3

Thread: read/write to textfile

  1. #1

    read/write to textfile

    I’m using this code to enter information in a txtfile. All the information is entered on one line. How can I enter information on the second line without reentering the same code?


    [vba]Open ObscurePath & ObscureFile For Output As #1
    Print #1, StartTime; vbTab; systID; vbTab; lbPassWord
    Close #1[/vba]
    Once the information is entered on the second line, how do I read the second line. The code I’m using to read the first line is

    [vba]

    f = fs.OpenTextFile(ObscurePath & ObscureFile).readall
    Split(f, vbTab)(1)
    [/vba]

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Here is one method to append data.
    [vba]Function AppendToTXTFile1(strFile As String, strData As String) As Boolean
    Dim iHandle As Integer, l As Long
    iHandle = FreeFile
    Open strFile For Append Access Write As #iHandle
    Print #iHandle, strData
    Close #iHandle
    AppendToTXTFile1 = True
    End Function[/vba]
    You can use Split and split by vbcrlf if you want to parse by lines.
    e.g.
    [VBA]Sub ImportMyFile()
    Dim s As String, a() As String
    s = "X:\txtfiles\MyFile.txt"
    s = OpenTextFileToString(s)
    a() = Split(s, vbCrLf)
    Debug.Print a(0), a(1), a(2)
    Worksheets("Sheet1").Range("A1").Resize(UBound(a) - LBound(a) + 1, 1).Value = WorksheetFunction.Transpose(a)
    End Sub[/VBA]

  3. #3
    Thank you Kenneth.

Posting Permissions

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