| Public Function WrapString(ByVal sString As String, _
Optional nMaxWidth = 72, Optional nMargin = 10, _
Optional nIndent = 0) As String
'sString = The string to be wrapped
'nMaxWidth = The maximum width of any line
'nMargin = The maximum number of characters to be
' dropped to the next line
'nIndent = Additional space to be added to new lines
'Verify that the values make sense
Debug.Assert nMaxWidth > 1
Debug.Assert nMargin > 0
Debug.Assert nMargin < nMaxWidth
Debug.Assert nIndent >= 0
Debug.Assert nIndent < nMaxWidth
Dim sBreakChars As String
sBreakChars = " -" 'These are the characters to word-wrap on
Dim i As Long
Dim bFound As Boolean
Do Until Len(sString) < nMaxWidth
'Look for a word wrap character
bFound = False
For i = nMaxWidth To nMaxWidth - nMargin Step -1
If InStr(1, sBreakChars, Mid(sString, i, 1)) > 0 Then
'Found one, so just break out of the loop
bFound = True
Exit For
End If
Next
'If no word wrap character was found, then just use the
' margin size
If Not bFound Then
i = nMaxWidth
End If
'If there's already a line, append a newline character
If WrapString <> "" Then
WrapString = WrapString & vbNewLine
End If
'And now add the next line, and chop it off of our work string
WrapString = WrapString & Mid(sString, 1, i)
sString = Mid(sString, i + 1)
'Add the indention spaces if necessary
If sString <> "" Then
sString = Space(nIndent) & sString
End If
Loop
'If there's still something left over,
' then add it to the final output
If sString <> "" Then
If WrapString <> "" Then
WrapString = WrapString & vbNewLine
End If
WrapString = WrapString & sString
End If
End Function
|