Option Explicit
Public Function GetMultilineTextBoxText(ByVal TextBox As Object, Optional ByVal TrimLeft As Boolean) As String
Dim I As Long, lLineCount As Long, lNewSelLength, lNewSelStart As Long, lPrevX As Long
Dim sLinesArray() As String
Dim sLineText As String
On Error Resume Next
CallByName TextBox, "Activate", VbMethod
CallByName TextBox, "SetFocus", VbMethod
On Error GoTo 0
With TextBox
If .LineCount > 1 Then
lNewSelStart = 0
For I = 1 To Len(.Text) + 1
.SelStart = lNewSelStart
.SelLength = lNewSelLength
sLineText = .SelText
If .CurTargetX < lPrevX Then
ReDim Preserve sLinesArray(lLineCount)
sLinesArray(lLineCount) = IIf(TrimLeft, LTrim(sLineText), sLineText)
lNewSelStart = I - 1
lNewSelLength = 0
lLineCount = lLineCount + 1
End If
lPrevX = .CurTargetX
lNewSelLength = lNewSelLength + 1
Next I
If lLineCount Then
ReDim Preserve sLinesArray(UBound(sLinesArray) + 1)
sLinesArray(UBound(sLinesArray)) = .SelText
Else
GetMultilineTextBoxText = Replace(.Text, vbLf, "")
Exit Function
End If
.SelStart = 0
.SelLength = 0
Selection.Select
GetMultilineTextBoxText = Join(sLinesArray, vbLf)
Else
GetMultilineTextBoxText = .Text
End If
End With
End Function