How to wrap words per line by number of characters (Word Wrap feature)
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 'String Manipulation - How to wrap words per line by number of characters (Word Wrap feature) Option Explicit 'This code snippet will allow you to limit the number of words per line based on a length. 'For Example: If you have 96 characters per line it will figure out how many words 'will fit on that line until it wraps. 'RichTextbox1.Text = WordWrap(RichTextbox1.Text, 96) Public Function WordWrap(ByVal strExpression As String, ByVal lLength As Long) As String Dim BufferCrLf() As String, BufferSpace() As String, Buffer As String Dim K As Long, J As Long, iCount As Long BufferCrLf() = Split(strExpression, vbCrLf) For K = LBound(BufferCrLf()) To UBound(BufferCrLf()) If Len(BufferCrLf(K)) <= lLength Then Buffer = Buffer & BufferCrLf(K) & vbCrLf Else BufferSpace() = Split(BufferCrLf(K), " ") For J = 0 To UBound(BufferSpace()) iCount = iCount + Len(BufferSpace(J)) + 1 If (iCount <= lLength) Then Buffer = Buffer & BufferSpace(J) & " " Else iCount = 0 Buffer = Buffer & vbCrLf & BufferSpace(J) & " " iCount = Len(BufferSpace(J)) + 1 End If Next J Buffer = Buffer & vbCrLf End If Next K WordWrap = Buffer End Function |