Deleting all of a style in word

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
aaabbbccc180
Posts: 5
Joined: 29 Nov 2021, 18:03

Deleting all of a style in word

Post by aaabbbccc180 » 05 Dec 2021, 13:01

Hi all,

Looking to delete all instances of a style in a word document. The basic function is easy enough to implement in VBA, by simply finding one instance of the style, using WordBasic to select similar formatting, and then deleting the selection:

Code: Select all

Sub SelectAllAnalytics()
'
' SelectAllAnalytics Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("analytic real")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    WordBasic.SelectSimilarFormatting
    Selection.Delete
However, I have to do this to several styles in a row, and not every style is guaranteed to be present in the document. This is a problem because when Selection.Find doesn't find anything, the SelectSimilarFormatting function just selects and deletes all instances of the style selected by the cursor.

I need the last two lines of the code not to trigger if Selection.Find.Style finds nothing. I think this should be possible to do with COM objects but don't know how. Anyone have ideas?

The only other idea I had was to use keystrokes to go to the end of the document, hit return, and apply the style to an empty line to make sure the macro always finds something, but this is clunky and slow.

Thanks in advance.

Return to “Ask for Help (v1)”