Microsoft Word VBA: Insert Carriage Return after set character amount

7.4k views Asked by At

I'm trying to create a VBA that will enter a carriage return after the text has reached 3800 characters. Ideally the script would stop at the beginning of the last word and enter a new return, but it would be tremendous just to have the carriage returns.

Any assistance would be greatly appreciated.

Thank you in advance!

1

There are 1 answers

0
Kazimierz Jawor On

Please remember, that in word even paragraph sign belongs to characters collections.

Here is the code:

Sub Solution()

    Dim i as integer
    i = 3800

        If ActiveDocument.Range(i - 1, i) = " " Then
            ActiveDocument.Range(i, i).InsertBefore Chr(11)
        Else
            ActiveDocument.Range(i + 1, i + 1).InsertAfter Chr(11)
        End If

End Sub

You can consider replacing Chr(11) with Chr(13).