empty paragraph

H

Harvey

Hi everyone,
I want to change font name,font size , space before and after each
paragraph in each document in a folder with hundreds of documents( now they
have different fonts and sizes so i want them to look the same). Because in
some documents there are extra empty paragraphs first i need to remove them
then apply formatting and here is where i have a problem I used two method
for removing empty paragraphs first find /replace ^p^p with ^p and also the
following macro

Dim parag As Paragraph
For Each parag In ActiveDocument.Paragraphs
With parag.Range
If .Words.Count = 1 Then
parag.Range.Delete
End If
End With
Next parag

For some reason ^P^P method is not able to remove all the empty paragraphs (
move most)
anythoughts?
 
H

Helmut Weber

Hi Harvey,

replace ^p^p replaces a paragraph mark,
followed by a paragraph mark, by a paragraph mark.

If the doc starts with an empty paragraph
and is followed by a non empty paragraph,
the condition paragraph mark followed by paragraph mark
does not apply.

As to the doc's end, you wont get rid of a trailing
empty paragraph by replacing ^p^p by ^p.

The last paragraph mark is something special.

Replace ^p^p with ^p would reduces
four empty paragraphs to two empty paragraphs,
six empty paragraphs to three empty paragraphs.

The code you have posted should do all you want,
no extras required.
Dim parag As Paragraph
For Each parag In ActiveDocument.Paragraphs
With parag.Range
If .Words.Count = 1 Then
parag.Range.Delete
End If
End With
Next parag

Any problems with that?

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
R

Russ

Harvey,
Maybe you wanted to use:
If .Characters.Count = 1 Then
I made this macro to **quickly** delete empty lines in a list made up of
paragraphs.
You could try it on a test document to be sure that it doesn't mess with
your paragraph formatting, etc. If you do try it, I would be interested in
how it worked for you.

Public Sub Delete_Empty_Lines()
Set objRange = ActiveDocument.Range(0, 0)
With objRange.Find
.MatchWildcards = True
.Text = "^13" 'MacWord users substitute \n for ^13
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = "^13{2,}"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
End With
Do While ActiveDocument.Paragraphs.Last.Range.Characters.Count = 1
ActiveDocument.Paragraphs.Last.Range.Delete
Loop
Do While ActiveDocument.Paragraphs.First.Range.Characters.Count = 1
ActiveDocument.Paragraphs.First.Range.Delete
Loop
End Sub

I also usually call it between screen updating modes to speed things up:
Application.ScreenUpdating = False
....
Call Delete_Empty_Lines
....
Application.ScreenUpdating = True
 
G

Greg Maxey

Another way (I don't know if it is any better or faster) that doesn't have
to calculate the paragraph count with each loop is:

Public Sub AnotherWay()
Dim aPara As Range
With ActiveDocument.StoryRanges(wdMainTextStory)
Set aPara = .Paragraphs(1).Range
Do
If Len(aPara.Text) = 1 Then
aPara.Delete
Else
aPara.Collapse wdCollapseEnd
aPara.MoveEnd wdParagraph, 1
End If
Loop Until aPara.End = .End
End With
If Len(aPara.Text) = 1 Then aPara.Delete
Set aPara = Nothing
End Sub
 
R

Russ

Greg,
That's another perspective.
However, your sub might not have any advantage over Harvey's original 'for
each' loop, since he is not using a counter. My sub uses the wdReplaceAll,
but may do weird things with his paragraph formatting, as Helmut once
pointed out. That's why if Harvey tries it, I would be interested in some
feedback.
 
G

Greg Maxey

Russ,

Right he isn't using a counter but I think that the processor has to
recalculate the collection of paragraphs each time the procedure cycles
through a For Each parag in ActiveDocument.Paragraphs.

While is likely makes no difference in most cases, going through a document
the size of War and Peace say there may be some efficiencies gained ;-) Of
course that is mute in cases where a ReplaceAll like your method. However
if something needs to be done to every paragraph that is a different story.
 
H

Harvey

Thanks everyone !
--
Best regards,
Harvey


Greg Maxey said:
Russ,

Right he isn't using a counter but I think that the processor has to
recalculate the collection of paragraphs each time the procedure cycles
through a For Each parag in ActiveDocument.Paragraphs.

While is likely makes no difference in most cases, going through a document
the size of War and Peace say there may be some efficiencies gained ;-) Of
course that is mute in cases where a ReplaceAll like your method. However
if something needs to be done to every paragraph that is a different story.




--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
 

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