Deleting Paragraphs with the same style

R

Ruth

Can someone help me with VBA code to delete all paragraphs in a document with
a style called "wills comments".
Thanks
 
T

Tom Winter

This should get you started:

This code is untested, but you get the idea:

####

Do

Set oRange = FindRangeWithStyle(oDocument.Content, "StyleName")

If Not oRange Is Nothing Then

oRange.Delete

End if

While Not oRange Is Nothing

#####

The code below has been tested:

#############

Public Function FindRangeWithStyle(oSearchRange As Word.Range, sStyleName As
String) As Word.Range

' This searches oSearchRange for the first group of contiguous
paragraphs with the
' same style.

On Error GoTo ErrorHandler

Dim oFirstParagraph As Word.Paragraph

Dim lParaCount As Long

Dim oResultRange As Word.Range

' This finds the first paragraph with the style.

Set oFirstParagraph = FindParaWithStyle(oSearchRange.Paragraphs,
sStyleName)

If Not oFirstParagraph Is Nothing Then

' Then we count how many paragraphs after that one have the
style.

lParaCount = CountParasWithStyle(oFirstParagraph, sStyleName)

Set oResultRange = oFirstParagraph.Range.Duplicate

oResultRange.MoveEnd wdParagraph, lParaCount - 1

Set FindRangeWithStyle = oResultRange

Else

Set FindRangeWithStyle = Nothing

End If

Exit Function

ErrorHandler:

' Do something appropriate...

End Function

Public Function FindParaWithStyle(oParagraphs As Word.Paragraphs, sStyleName
As String) As Word.Paragraph

On Error GoTo ErrorHandler

Dim oCurrParagraph As Word.Paragraph

Dim oCurrStyle As Word.Style

For Each oCurrParagraph In oParagraphs

Set oCurrStyle = oCurrParagraph.Style

If StrComp(oCurrStyle.NameLocal, sStyleName, vbTextCompare) = 0
Then

Set FindParaWithStyle = oCurrParagraph

Exit Function

End If

Next

Set FindParaWithStyle = Nothing

Exit Function

ErrorHandler:

' Do something appropriate...

End Function

Public Function CountParasWithStyle(oStartParagraph As Word.Paragraph,
sStyleName As String) As Long

On Error GoTo ErrorHandler

Dim oCurrParagraph As Word.Paragraph

Dim oCurrStyle As Word.Style

Dim lParaCount As Long

lParaCount = 0

Set oCurrParagraph = oStartParagraph

Do While Not oCurrParagraph Is Nothing

Set oCurrStyle = oCurrParagraph.Style

If StrComp(oCurrStyle.NameLocal, sStyleName, vbTextCompare) <> 0
Then

Exit Do

End If

lParaCount = lParaCount + 1

Set oCurrParagraph = oCurrParagraph.Next

Loop

CountParasWithStyle = lParaCount

Exit Function

ErrorHandler:

' Do something appropriate...

End Function

#######
 
G

G.G.Yagoda

Wouldn't a for each paragraph loop be more efficient than finding the
style?

Sub KillWill()
MsgBox ActiveDocument.Paragraphs.Count, , "Before"
Dim P As Paragraph
For Each P In ActiveDocument.Paragraphs
If P.Style = "wills comments" Then P.Range.Delete
Next
MsgBox ActiveDocument.Paragraphs.Count, , "After"
End Sub

How does Will feel about all this?
 
G

Greg

Sub DeleteText1()

Dim oPara As Paragraph
Set oPara = ActiveDocument.Paragraphs(1)

Do
If oPara.Style = "wills comments" Then
oPara.Range.Delete
End If
Set oPara = oPara.Next
Loop Until oPara Is Nothing
End Sub
 
T

Tom Winter

Yes, you're probably right. ;) I just copied and pasted in some old code
that I had used for another purpose...-Tom
 
G

G.G.Yagoda

Greg -

I am just curious as to what is the practical advantage of re-setting
each paragraph object to the next one until the object is nothing as
opposed to a plain-jane "For Each paragraph" loop, although it must be
said that loop until nothing sounds and looks more sophisticated.
 
G

Greg Maxey

G.G.,

Monkey see, monkey do :). I had a awful time a few months back with using
a For Each construction and during the discussion one of the respondents
used this loop construction. I think it was Jezebel or maybe Word Heretic.
I don't remember. Perhaps that person will be along to over us both better
insight.


Greg Maxey/Word MVP
A Peer in Peer to Peer Support
 
T

Tom Winter

Well, like I said, I just copied and pasted some old code I had used for
something else. I didn't really think it out for this SPECIFIC application.
I thought the questioner might learn something useful from seeing the code
and various ways of working with styles, ranges and paragraphs.

Also, I usually don't like deleting something that I'm iterating over. With
some systems/objects/etc., it can cause problems if you delete a member of a
collection while you are iterating the collection. A minor point here
though. I imagine Greg's solution will work just fine, short and sweet.
 
G

G.G.Yagoda

So we still await a definitive disquisition on the subject of why Loop
Until Object is Nothing is superior to For Each Object, and why the
Loop Until Object is Nothing method doesn't touch some members of a
collection?

Students at the Monkey See Monkey Do Institute are eager for a lecture.
 
B

Bob S

So we still await a definitive disquisition on the subject of why Loop
Until Object is Nothing is superior to For Each Object, and why the
Loop Until Object is Nothing method doesn't touch some members of a
collection?

Students at the Monkey See Monkey Do Institute are eager for a lecture.

Hello, this is Monkey Hear Monkey Say calling Monkey See Monkey Do...

The reasons that I have heard for using Next-Until-Nothing rather than
For-Each:

1.

For large collections, For-Each allegedly becomes slower the farther
you get into the collection. The implication is that it walks down the
list from the beginning each time in order to find the "next" element.

Next-Until-Nothing starts from where you are, so it should not suffer
this problem.

(There have also been claims that for large collections a For-Each is
much faster than an indexed For-To loop.)

2.

According to the documentation, a For-Each loop is supposed to process
each member of the collection once, even if you alter the content of
the members in the loop. (I don't know if it is supposed to do it
"exactly once" or "at least once".) For example, if you do a For Each
Paragraph loop, it is supposed to do each paragraph even if you are
deleting some of the characters from the paragraph as part of the code
in the loop.

However, there have been several postings in the VBA groups that
appear to show that such loops may restart from the beginning if the
loop body alters the content of a member. This can result in the loop
never finishing, or making excessive alterations to the text.

Exactly what mechanism VBA is using to attempt to cause the For-Each
to hit each member exactly once is a mystery to me.

Whatever mechanism it uses is known to fail in the face of collection
re-shuffling caused by deleting a member; maybe it also fails in
certain other cases. I haven't seen an official list of what can cause
collection re-ordering.

Bob S
 

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