• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

Working with a text file

How would I read the 3rd line of a text file and put it into a variable?
0
pcarlow
Asked:
pcarlow
  • 4
1 Solution
 
JR2003Commented:
Include a reference to the "Microsft Scripting Runtime" by going into the Project/References menu option.

Private Sub Command1_Click()

    Dim fso As Scripting.FileSystemObject
    Dim MyTextStream As Scripting.TextStream
    Dim sLineText As String
    Dim i As Long

    Set fso = New Scripting.FileSystemObject
    Set MyTextStream = fso.OpenTextFile("c:\a.txt")
    For i = 1 To 3
        sLineText = MyTextStream.ReadLine
    Next i
    MsgBox sLineText
   
End Sub
0
 
BurbbleCommented:
Here you go:

-----
Private Sub Form_Load()
    Dim FileNo As Integer
    Dim strLine() As String
    Dim strTemp As String
    Dim intCounter As Integer

    intCounter = 0
    FileNo = FreeFile
    Open "C:\filename.txt" For Input As #FileNo
        Do While Not EOF(FileNo)
            Line Input #FileNo, strTemp
            ReDim Preserve strLine(intCounter)
            strLine(intCounter) = strTemp
            intCounter = intCounter + 1
        Loop
    Close #FileNo
End Sub
-----

-Burbble
0
 
BurbbleCommented:
Ah, two methods there ;-)

-Burbble
0
 
BurbbleCommented:
Oh, I should have mentioned that the third line would be:

strLine(2)

Since the array starts at 0.

-Burbble
0
 
BurbbleCommented:
Glad I could help, thanks for the points ;-)

-Burbble
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now