Log in

View Full Version : Find and replace paragraph spacing



krishnak
04-27-2007, 11:26 AM
Hi All,

I am trying to format a generated Word document by removing extra paragraph spacings. I recorded the macro by following the Find and Replace method. I want to loop it through the document so that all additional paragraph spacings are removed.

Before applying the loop, I wanted to test by manually operating a test document to ensure that all the additional paragraph marks are replaced. The code recognizes that two consecutive paragraph spacings are existing even after all the double paragraph spaces are replaced by single paragraph spacings. Maybe I am not refreshing the myRange content.

Please examine my code and see where I am going wrong. This code works fine for finding and replacing words.

Sub TestMacro()

Dim myRange As Range
Dim cntParas As Long
cntParas = ActiveDocument.Paragraphs.Count
Set myRange = ActiveDocument.Range(ActiveDocument.Paragraphs(1).Range.Start, _
End:=ActiveDocument.Paragraphs(cntParas).Range.Start)

With myRange.Find
.Forward = True
.Text = "^p^p"
' .ClearFormatting
.Replacement.Text = "^p"
' .Replacement.ClearFormatting
.Execute Replace:=wdReplaceAll

End With
ActiveDocument.Content.Select
If (myRange.Find.Text = "^p^p") Then

MsgBox ("Do again")
Else
MsgBox ("ALL DONE")
End If

lucas
04-27-2007, 11:57 AM
As Gerry would tell you....use styles instead of the enter key to add space between paragraphs and this would not be a problem...if you have inherited a document then that's different.

As to the code...it seems to do exactly what you tell it to do.....find all instances of "^p^p and replace them with ^p

If your trying to get rid of all of them try:
.Replacement.Text = ""

If that doesn't help you should post a small example with a little more explaination...

krishnak
04-27-2007, 01:54 PM
Thanks for the prompt reply. Yes, the code does the replacement alright. In the case of generated Word document which runs to 100 pages, the code does not do the job in one go. It has to be run several times to replace all the double (or more) paragraph spacings. I want to run this code in a loop, which will continue to run while .Find.Text = "^p^p".

I am attaching a test Word doc with the paragraph spaces created. After a couple of runs, I could replace all the double (or more) paragraph spaces, except the ones at the end of the doc. And if we test the condition for the doc, .Find.Text = "^p^p" is always true, even after we remove the double paragraphs at the end by hand. I want to use this condition for stopping the loop.

This piece of code is part of the code for other formatting as well. So this has to replace all the double (or more) paragraph spaces with a single paragraph in one run through a loop.

Hope that I explained the situation. If anyone requires additional details, I am happy to give them as well.

Thanks in advance.

fumei
04-27-2007, 07:54 PM
Of course it does require multiple passes.

One^p
^p
^p
^p
Two^p
^p
^p
^p

Pass 1:

One^p
^p
^p
^p
Four in a row.


becomes:

One^p
^p
^p
Three in a row.

The Replace does not replace all of them, it replaces the found ^p^p in order, and goes to the NEXT one. This leaves some behind.

OK. This code will in fact do what you want.Sub TestMacro2()
Dim opara As Word.Paragraph
Dim var
For Each opara In ActiveDocument.Paragraphs
If opara.Range.Text = Chr(13) Then
opara.Range.Delete
End If
Next
For var = ActiveDocument.Paragraphs.Count To 1 Step -1
ActiveDocument.Paragraphs(var).Range.InsertParagraphAfter
Next
End SubWhat it does.

Step 1: Make a paragraph object for each paragraph. If the paragraph text is Chr(13) - a paragraph mark - it deletes it.

Step 2: working backwards (VERY important!!!), it puts a blank paragraph after each paragraph.

Result?

One^p
^p
Two^p
^p
Three^p
^p
Four^p
^p

I would again very, very, very, very, strongly suggest you convert this to proper styles! I am sending the document back up, with the addition of a "BasicText" style. It is formatted to have 10 pts following. That is, a paragraph set with the style will have a 10 pts space after it. You could make it 12 pts, or any other number.

The point being is that using "empty" paragraphs to make space between paragraphs is BAD. The truth of the matter is that you are NOT making space between paragraphs, although it looks that way. You are adding paragraphs. There is a significant difference.

Sub UseStyles()
Dim opara As Word.Paragraph
For Each opara In ActiveDocument.Paragraphs
If opara.Range.Text = Chr(13) Then
opara.Range.Delete
Else
opara.Style = "BasicText"
End If
Next
End SubJust for a laugh, try running the UseStyles procedure.

krishnak
04-30-2007, 01:26 PM
Thanks for the tip to replace the paragraph marks by a space. I worked out my code on those lines, and the resulting document looks better. I am still working on other issues on the document.