PDA

View Full Version : limit characters per line to 69 and word wrap in text box



bigrig078
04-27-2016, 11:33 AM
Hello all! I got help on this forum from Greg (thanks Greg for all your help!) and he provided me with this code to limit the characters to 69 in a text box. I have been messing with the code for a week now and cannot figure out how to make it word wrap as well so that the total number of characters does not exceed 69 characters and that a word is carried over to the next line if it is more than 69 characters long. Here is the code I have....Please help!!! Is it possible to have a word wrap function incorporated into this code?


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim arrLines() As String
Dim lngIndex As Long
Dim k
k = KeyAscii
arrLines = Split(TextBox1.Text, Chr(10))
For lngIndex = 0 To UBound(arrLines)
If Len(arrLines(lngIndex)) > 69 Then
Debug.Print Asc(Right(arrLines(lngIndex), 1))
If Not Asc(Right(arrLines(lngIndex), 1)) = 13 Then
KeyAscii = 0
TextBox1.Text = TextBox1.Text & Chr(10) & Chr(k)
Beep
End If
End If
Next
End Sub