Skip to content

Instantly share code, notes, and snippets.

@lotusorrose
Created January 5, 2023 17:28
Show Gist options
  • Save lotusorrose/5a9e3ce5b2dfb02fe961375e83efe86b to your computer and use it in GitHub Desktop.
Save lotusorrose/5a9e3ce5b2dfb02fe961375e83efe86b to your computer and use it in GitHub Desktop.
Option Explicit
Dim maxChars As Long
Dim txt As String
Dim newtxt As String
Dim curcounter As Long
Public Sub main()
maxChars = 80
txt = ActiveDocument.Content.Text
txt = Replace(txt, vbNewLine, " ")
txt = Replace(txt, vbLf, " ")
txt = Replace(txt, vbCr, " ")
txt = Replace(txt, vbTab, " ")
txt = Replace(txt, " ", " ")
newtxt = ""
curcounter = 1
While curcounter < Len(txt)
If curcounter + maxChars < Len(txt) Then
newtxt = newtxt & vbCrLf & Mid(txt, curcounter, InStrRev(txt, " ", curcounter + maxChars) - curcounter)
Else
newtxt = newtxt & vbCrLf & Mid(txt, curcounter)
End If
curcounter = CLng(InStrRev(txt, " ", curcounter + maxChars))
If curcounter = 0 Then
curcounter = Len(txt)
End If
Wend
ActiveDocument.Content.Text = newtxt
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment