Simple Wordwrap Function

By | 2019-12-30

I hope that this is the shortest and easiest wordwrap-function in vb you have ever seen, that you enjoy it and use it in all your projects.

Option Explicit
Private Sub Form_Load()

   MsgBox WordWrap("This is a long testtext that doesn't make any sense really. But I hope you will enjoy my example and I do not know what I can write any more. This must be enough", 20), vbOKOnly + vbInformation, "WordWrap"

End Sub

Function WordWrap(ByVal Text As String, Optional ByVal MaxLineLen As Integer = 70)

   Dim i As Integer
   For i = 1 To Len(Text) / MaxLineLen
      Text = Mid(Text, 1, MaxLineLen * i - 1) & Replace(Text, " ", vbCrLf, MaxLineLen * i, 1, vbTextCompare)
   Next i
   WordWrap = Text

End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.