If the file was a Random Access File, (All Lines/Records are the Same Length) then you could use the Open.. For Random Access Read Statement and the Get Method to randomly pick a line from the File.
Otherwise, you will need to read the whole file in, parse it into it's individual lines, then seelct one at random, eg.
Code:Dim iFile As Integer Dim iPos As Long Dim sFile As String dim iLines As Integer Dim aLines() As String iFile = FreeFile Open "C:\MyFile.txt" For Input As iFile sFile = Input(LOF(iFile), iFile) Close iFile While Len(sFile) iPos = Instr(sFile, vbCrLf) If iPos = 0 Then iPos = Len(sFile) + 1 ReDim Preserve aLines(iLines) aLines(iLines) = Left(sFile, iPos - 1) sFile = Mid$(sFile, iPos + 2) iLines = iLines + 1 Wend Randomize Timer Msgbox aLines(Int(Rnd * Ubound(aLines)))
------------------
Aaron Young
Analyst Programmer
[email protected]
[email protected]




Reply With Quote