Insert Line Break every 215 Characters

A

Ahmed Mesbah

Hello Group!

Is it possible to have Word insert a line break automatically after every
215 characters (spaces inclusive)?

Thanks in advance.

Ahmed
 
H

Helmut Weber

Hi Ahmed,

like that:

Sub Test6002()
Dim rngDcm As Range
Set rngDcm = ActiveDocument.Range
With rngDcm.Find
.Text = "?{215}"
.MatchWildcards = True
While .Execute
rngDcm.InsertAfter Chr(11)
rngDcm.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Gregory K. Maxey

Helmut,

Interesting. I would have done it like this:
Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
Do While oRng.End - oRng.Start > 215
oRng.MoveStart wdCharacter, 215
oRng.InsertBefore Chr(11)
Loop
End Sub
 
J

Jean-Guy Marcil

Gregory K. Maxey said:
Helmut,

Interesting. I would have done it like this:
Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
Do While oRng.End - oRng.Start > 215
oRng.MoveStart wdCharacter, 215
oRng.InsertBefore Chr(11)
Loop
End Sub

Hi Greg,

I like the range solution, but, you have to tweak it a bit. When you add
Chr(11), it becomes part of the range, so at the next pass, you would have to
move the start by 216, not 215.

Try something like this instead:

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng
Do While .End - .Start > 215
.MoveStart wdCharacter, 215
.InsertBefore Chr(11)
.MoveStart wdCharacter, 1
Loop
End With
 
H

Helmut Weber

Hi Greg,
Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
Do While oRng.End - oRng.Start > 215
oRng.MoveStart wdCharacter, 215
oRng.InsertBefore Chr(11)
Loop
End Sub

which results in a length of 214! ;-)

Have a nice day.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
A

Ahmed Mesbah

Helmut, Greg and Jean,

Thank you VERY much. It did the trick. You've been a huge help!

Have a great day!

Ahmed

Hi Greg,
Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
Do While oRng.End - oRng.Start > 215
oRng.MoveStart wdCharacter, 215
oRng.InsertBefore Chr(11)
Loop
End Sub

which results in a length of 214! ;-)

Have a nice day.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top