Word VBA - delete all text of Style("StyleToDelete")

Sorry, newbie question!
In a 500 page Word document, I want to delete all text that I have assigned the Style "StyleToDelete".

Thanks
hindersalivaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Make a backup copy first.
Sub DeleteStyle()
Dim rng As Range

Set rng = ActiveDocument.Content

With rng.Find
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .ClearFormatting
    .Style = ActiveDocument.Styles("Heading 3")
    Do While .Execute
        rng.Words(1) = ""
        rng.Words(1).Style = "Normal"
        rng.Collapse Direction:=wdCollapseEnd
    Loop
End With

End Sub

Open in new window

1
hindersalivaAuthor Commented:
Hi Martin. I was hoping there’s a ‘Select all text with Style(“deletethis”)’ and then delete. Like a GoTo in Excel?
‘Finding’ them I know will take at least 30 seconds.
0
abbas abdullaCommented:
Hi,

The option to find styles is there in word. ( I tested the below in Word 2016)

1. Goto Find and Replace - Shortcut Ctrl+H
2. From the format button choose style - For example Heading 1 and click OK.
3. Put something into Replace with text box or keep it empty if you want to replace with nothing.
4. Click Replace All.

See the attached pic.
Word_FindStyleAndReplaceAll.PNG
0
Martin LissOlder than dirtCommented:
The key is wdReplaceAll
Sub DeleteAllStyle()

Dim StyleToDelete As String
StyleToDelete = "Heading 3"

    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles(StyleToDelete)
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.