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