Perhaps this is not exactly what the OP had in mind, but I decided that I would share my VBA word wrap function, since I could not find anything on the Internet that could do what I wanted.
This function inserts CR + LF in a line to transfer it , so word wrap is preserved if the text is copied to another application, text or otherwise.
Function wrapText(strIn As String, Optional maxLen As Long = 110) As String Dim p As Long: wrapText = strIn Do p = InStrRev(wrapText, " ", p + maxLen) - 1 wrapText = Left(wrapText,p) & vbCrLf & Right(wrapText, Len(wrapText)-p-1) Debug.Print Mid(Replace(wrapText, vbCrLf, "||"), p - 20) 'Stop Loop While p + maxLen < Len(wrapText) End Function
By default, the maximum width is 115 characters, but you can change it to anything if you wish. It breaks only into spaces (the last that appears in / before position # 115) and inserts only CR + LF (with the constant vbCrLf
), but they can be adapted as needed.
As an example of an application, I built complex SQL queries in Excel and wanted to carefully and accurately copy SQL to a server application instead of one giant line.
source share