Link to home
Start Free TrialLog in
Avatar of hindersaliva
hindersalivaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

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

Avatar of hindersaliva

ASKER

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.
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
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial