WrapString |
| Description: | |
| This is a simple function that word wraps a string into smaller strings, separating each line with a newline. | |
| Code: | |
Public Function WrapString( _
sLineToWrap As String, _
Optional nFirstLine As Long = 5, _
Optional nOtherLines As Long = 0, _
Optional nLineLength As Long = 72) As String
Dim bFirst As Boolean
Dim nPos As Long
Dim bBreakFound As Boolean
bFirst = True
Do While Len(sLineToWrap) > nLineLength
nPos = nLineLength
bBreakFound = False
Do Until nPos = 0 Or bBreakFound
If Mid(sLineToWrap, nPos, 1) = " " Then
bBreakFound = True
ElseIf Mid(sLineToWrap, nPos, 1) = "-" Then
bBreakFound = True
Else
nPos = nPos - 1
End If
Loop
If nPos = 0 Then
nPos = nLineLength
End If
WrapString = WrapString & Space(IIf(bFirst, nFirstLine, _
nOtherLines)) & Mid(sLineToWrap, 1, nPos) & vbNewLine
sLineToWrap = Mid(sLineToWrap, nPos + 1)
bFirst = False
Loop
WrapString = WrapString & Space(IIf(bFirst, _
nFirstLine, nOtherLines)) & sLineToWrap
End Function
| |
| Sample Usage: | |
Dim sLine As String
sLine = "This is a really long continuous line that I wrote to "
sLine = sLine & "demonstrate the wrapping capabilities of the "
sLine = sLine & "WrapString function. This text serves no "
sLine = sLine & "other purpose other than as an example for "
sLine = sLine & "this function, feel free to ignore this text."
Debug.Print WrapString(sLine)
| |