changing paragraphs ect.

J

Jim

Hi folks,
I'm a newbee, so please bear with me...
-*- Office 2002, Windows XP -*-

I have a long document typed into word with this structure;

Q"tabchar"Sentence goes here. If this sentence is longer than 60
characters it wraps to the next line. However, I have to save this
document as plain ASCII text and need the lines to all line up using that
tab. A"tabchar"Some answer to the above question

I need to save this document as ASCII text and have the sentences/paragraphs
all line up with tabs like this;

Q"tabchar"Sentence goes here. If this sentence is longer than 60
"tabchar"characters it wraps to the next line. However, I have to
"tabchar"save this document as plain ASCII text and need the lines
"tabchar"to all line up using that tab.

Currently I have to enter the tabs manually any time the sentence/paragraph
wraps. This can be tedious to say the least...

Here is what I think needs to happen.
* For each paragraph, if the paragraph is longer than 60 characters, find
the "space" character closest, but not over 60 charcters, Delete the space
and insert, in it's place, a CR and TAB.

Too bad one can't write VB code like that...:)
If I record a macro and clean it up a bit this is what I get;

Selection.TypeBackspace
Selection.TypeParagraph
Selection.TypeText Text:=vbTab

How do I wrap this in code to loop through the document's paragraphs,
counting the characters, finding the correct space between words, deleting
the space and inserting the CR and TAB?

Thanks for any help,
Jim
 
C

Chad DeMeyer

Jim, something like this:

Sub CleanForAscii()
Dim oPar As Paragraph
Dim oRng As Range
Dim lLen As Long
Dim iCt As Integer
Dim lPos As Long
Dim i As Integer
Dim oChar As Range

For Each oPar In ActiveDocument.Paragraphs
Set oRng = oPar.Range
lLen = Len(oRng.Text)
lPos = 0
Do While lLen > 60
iCt = CInt(lLen / 60)
For i = 1 To iCt
lPos = lPos + 61
Do
lPos = lPos - 1
Set oChar = oRng.Characters(lPos)
Loop Until oChar.Text = " "
oChar.InsertAfter vbCr & vbTab
lPos = lPos + 2
Next i
lLen = lLen + (2 * iCt) - lPos
Loop
Next oPar

Set oRng = Nothing
Set oChar = Nothing
End Sub

Regards,
Chad
 
M

microsoft

Chad DeMeyer said:
Jim, something like this:

Chad,
YES! That worked. The only thing I had to do was change the following line,
iCt = CInt(lLen / 60)

to

iCt = Int(1Len / 60)

As it turns out (you probably already know this) CInt always rounds to
nearest int, so if the length of the paragraph ended up being 94, it would
give the value of 2 (94 / 60 = 1.56666 rounded to the nearest int = 2) to
iCt when it actually needs to be 1.
It took me a while to figure this out using the debugger (remember I'm a
newbee), but after I changed it, it works wonderfully.

Thank you,
Jim
 
C

Chad DeMeyer

Jim,

I confess that I made the assumption it always crops the decimal portion
rather than rounding, and did not confirm that my assumption was correct.
Thanks for the feedback.

Regards,
Chad
 

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