Move right and check for LF/CR one character at a time macro

B

BRC

I am trying to write a macro that will process a block of text that is
written in Word. The macro will check for a hard line feed every 74
characters and if it doesn't encounter one it will insert a lf.
This is to paste text into an old mainframe application that doesn't have
word wrap. Unless there is a line feed or CR at least every 74 characters.
The lines can be shorter but not longer then 74. Any help that might put me
on the correct path is appreciated. I believe it could be done with the do
while function but I am not sure how to check for value of the characters as
you use selection.moveright ..etc. thanks for any help
BRC
 
D

Doug Robbins - Word MVP

Hi BRC,

I believe the following code does what you want:

Dim arange As Range, i As Integer, j As Integer
i = 0
j = ActiveDocument.Paragraphs.Count
While i < j
Set arange = ActiveDocument.Paragraphs(i + 1).Range
If Len(arange) > 74 Then
arange.End = arange.Start + 74
While Not Right(arange, 1) = " "
arange.End = arange.End - 1
Wend
arange.InsertAfter vbCr
j = j + 1
End If
i = i + 1
Wend

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

Jeff Hall

Does that work ok?

If you are adding VbCr characters, are you not changing the number of
paragraphs in the activedocument and thus the value of j?
 
D

Doug Robbins - Word MVP

Hi Jeff,

Yes, whenyou insert a VbCr, you do increase the number of paragraphs in the
document. If j was not increased, the operation would only be performed on
the original number of paragraphs in the document.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

Jeff Hall

OIC I misinterpreted your use of j = j + 1 as being for the purpose of
stepping through the paragraphs collection when you are actually using i for
that. My mistake.
 

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