Count lines of text:
Get a specific line of text:
Code:
Public Function CountLines(InputText As String) As Integer
Dim TempCount, NewLinePos As Integer
TempCount = 0
If LenB(InputText) > 0 Then
InputText = Replace(InputText, vbCrLf, vbLf, , , vbBinaryCompare)
TempCount = 1
NewLinePos = InStr(1, InputText, vbLf, vbBinaryCompare)
While NewLinePos > 0
TempCount = TempCount + 1
NewLinePos = InStr(NewLinePos + 1, InputText, vbLf, vbBinaryCompare)
Wend
End If
CountLines = TempCount
End Function
Code:
Public Function GetLine(InputText As String, LineNo As Integer) As String
Dim TempLine As String
Dim CharPos, OldcharPos, TempLineCount As Integer
CharPos = 1
OldcharPos = 1
TempLineCount = 1
If LineNo < 0 Or LineNo > CountLines(InputText) Then
TempLine = ""
Else
If Right(InputText, Len(InputText)) <> vbLf Then InputText = InputText & vbLf
InputText = Replace(InputText, vbCrLf, vbLf, , , vbTextCompare)
For TempLineCount = 1 To LineNo
CharPos = InStr(OldcharPos, InputText, vbLf, vbTextCompare)
TempLine = Mid(InputText, OldcharPos, CharPos - (OldcharPos - 1))
OldcharPos = CharPos + 1
Next TempLineCount
'clear out any LF & CRs
TempLine = Replace(TempLine, vbCr, "", , , vbTextCompare)
TempLine = Replace(TempLine, vbLf, "", , , vbTextCompare)
End If
GetLine = TempLine
End Function