Delete All Paragraphs This Style (macro submission)

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15636
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Delete All Paragraphs This Style (macro submission)

Post by ChrisGreaves »

Code: Select all

Sub DeleteAllParagraphsThisStyle()
    ''' Place the text cursor in a paragraph, run the macro.
    ''' All paragraphs in that (selected) style will be removed from the document.
    Dim strStyleName As String
    strStyleName = Selection.Paragraphs(1).Range.Style
    Dim lng As Long
    For lng = ActiveDocument.Paragraphs.Count To 1 Step -1
        If ActiveDocument.Paragraphs(lng).Style = strStyleName Then
            ActiveDocument.Paragraphs(lng).Range.Delete
        Else
        End If
    Next lng
End Sub
There's nothing heavier than an empty water bottle

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Delete All Paragraphs This Style (macro submission)

Post by macropod »

Hi Chris,

You do realise you can do this with an ordinary Find/Replace, don't you?
Paul Edstein
[Fmr MS MVP - Word]

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15636
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Delete All Paragraphs This Style (macro submission)

Post by ChrisGreaves »

macropod wrote:You do realise you can do this with an ordinary Find/Replace, don't you?
Hi Macropod, and yes, thanks, I've been doing it that way for years.
This past week I've been faced with abstracting nuggets of text from a set of about 300 documents, and I started using Edit/Replace, then realized that I was always staring at the sort of crud that I didn't want.
I wrote an edit/replace macro along the usual lines, then realized that a generic tool might be more useful.
The macro you guys don't get to see is the one that deletes all paragraphs whose name matches the characters of the selected paragraph style, so running the macro from a "Body Text" styled paragraph will delete all paragraphs "Body Text", "Body Text2", "Body Text Bullet", "Body TextBullet" etc.
There's nothing heavier than an empty water bottle

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Delete All Paragraphs This Style (macro submission)

Post by macropod »

ChrisGreaves wrote:
macropod wrote:You do realise you can do this with an ordinary Find/Replace, don't you?
Hi Macropod, and yes, thanks, I've been doing it that way for years.
This past week I've been faced with abstracting nuggets of text from a set of about 300 documents, and I started using Edit/Replace, then realized that I was always staring at the sort of crud that I didn't want.
I wrote an edit/replace macro along the usual lines, then realized that a generic tool might be more useful.
The macro you guys don't get to see is the one that deletes all paragraphs whose name matches the characters of the selected paragraph style, so running the macro from a "Body Text" styled paragraph will delete all paragraphs "Body Text", "Body Text2", "Body Text Bullet", "Body TextBullet" etc.
Hi Chris,

I think you'd still find that Find/Replace is more efficient than looping through all paragraphs. For example:

Code: Select all

Sub Demo()
Application.ScreenUpdating = False
Dim oSty As Style
With ActiveDocument
  For Each oSty In .Styles
    If InStr(oSty.NameLocal, "Body Text") > 0 Then
      With .Range.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Style = oSty.NameLocal
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .Execute Replace:=wdReplaceAll
      End With
    End If
  Next oSty
End With
Application.ScreenUpdating = True
End Sub
Paul Edstein
[Fmr MS MVP - Word]

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15636
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Delete All Paragraphs This Style (macro submission)

Post by ChrisGreaves »

macropod wrote:I think you'd still find that Find/Replace is more efficient than looping through all paragraphs.
(sigh!) Thanks Paul. As usual, you're right (grin!).

I made a small change which does not invalidate your argument:

Code: Select all

''''''        If InStr(oSty.NameLocal, strStyleName) > 0 Then
        If UCase(Left(oSty.NameLocal, Len(strStyleName))) = UCase(strStyleName) Then
I wanted to test generically, from the left-hand side, rather than embedded within the name.
There's nothing heavier than an empty water bottle

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Delete All Paragraphs This Style (macro submission)

Post by macropod »

Hi Chris,

Why not:

Code: Select all

If InStr(oSty.NameLocal, strStyleName) = 1 Then
or, if you're concerned with case-sensitivity:

Code: Select all

If InStr(oSty.NameLocal, strStyleName, 0) = 1 Then
Paul Edstein
[Fmr MS MVP - Word]

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15636
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Delete All Paragraphs This Style (macro submission)

Post by ChrisGreaves »

macropod wrote:Why not:

Code: Select all

If InStr(oSty.NameLocal, strStyleName) = 1 Then
Hi Paul.
I'm stumped.
I can't come up with a good answer (grin)!

Thanks.
There's nothing heavier than an empty water bottle