PDA

View Full Version : read/write to textfile



av8tordude
07-24-2011, 06:25 PM
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?


Open ObscurePath & ObscureFile For Output As #1
Print #1, StartTime; vbTab; systID; vbTab; lbPassWord
Close #1
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



f = fs.OpenTextFile(ObscurePath & ObscureFile).readall
Split(f, vbTab)(1)

Kenneth Hobs
07-24-2011, 07:14 PM
Here is one method to append data.
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
You can use Split and split by vbcrlf if you want to parse by lines.
e.g.
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

av8tordude
07-25-2011, 08:05 AM
Thank you Kenneth. :friends: