hobbybrouwer
hobbybrouwer

Reputation: 21

Microsoft Word VBA: Insert Carriage Return after set character amount

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!

Upvotes: 2

Views: 7835

Answers (1)

Kazimierz Jawor
Kazimierz Jawor

Reputation: 19077

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).

Upvotes: 1

Related Questions