View Single Post
  #4 (permalink)  
Old August 18th, 2006, 11:45 AM
BrianWren BrianWren is offline
Friend of Wrox
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts

Try including all permutations of ways to make a line feed:
Public Function GetLine(ByVal MultiLineText As String, _
               Optional ByVal LineNo As Integer = 0 _
                       ) As String

    Dim arrSentences() As String ' Create a String Array

    If Trim(MultiLineText) = vbNullString Then Exit Function

    ' Normalize any alternates
    ' First make sure there is only one type of character
    MultiLineText = Replace(MultiLineText, VbCr, VbLf)

    ' Now you might have single instances of VbLf, or cases
    ' of VbLf & VbLf where you previously had VbCrLf.
    ' Eliminate duplicates.
    MultiLineText = Replace(MultiLineText, VbLf & VbLf, VbLf)

    ' Now do the split
    ' Split the entire text into a array
    ' considering Line Feeds as the delimiter
    arrSentences = Split(Trim(MultiLineText), vbLf)

    ' Now return the right element out of the array (zero-based)
    GetLine = arrSentences(LineNo)

End Function
Private Sub Command1_Click()

    MsgBox GetLine(Text1.Text, 3) ' To Retrieve the 3rd Line
    MsgBox GetLine(Text1.Text, 7) ' To Retrieve the 7th Line
    MsgBox GetLine(Text1.Text)    ' To Retrieve the 1st (that is, the zeroeth) Line

End Sub
Reply With Quote