We help IT Professionals succeed at work.

Macro in Word to Search by Named Style

oaktrees
oaktrees asked
on
Medium Priority
56 Views
Last Modified: 2020-03-16
I've got a style that I do lots of commenting in.  

Microsoft Word has got a search by style built-in, but it's pretty clunky.

Is there any way to modify this Macro to have it search for a named style?

The style name I'm using is

chrAComms

And it is a character style.

Sub aaag_HeaderDown()
On Error GoTo ErrorHandler
Selection.GoTo What:=wdGoToHeading, Which:=wdGoToNext, Count:=1, Name:=""
ErrorHandler:
End Sub

Sub aaah_HeaderUp()
On Error GoTo ErrorHandler
Selection.GoTo What:=wdGoToHeading, Which:=wdGoToPrevious, Count:=1, Name:=""
ErrorHandler:
End Sub

Open in new window


My thinking here is that f I can get a Macro to run the style search, I can set two keyboard shoorcuts and blaze through to the comments I need to see.

Many thanks!!

OT
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Hi OT. See if this works for you:
Sub aaag_HeaderDown()
On Error GoTo ErrorHandler
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("chrAComms Char")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
ErrorHandler:
End Sub

Sub aaah_HeaderUp()
On Error GoTo ErrorHandler
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("chrAComms Char")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = False
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
ErrorHandler:
End Sub
Paul

Author

Commented:
Hi Flyster!
Don't know what I'm doing wrong.  Tried to add.  Gave them separate shortcuts.  No response.
Tried to just Run them - the same.
What do you think I'm doing wrong?
Gave them new names.
Sub aaai_JumpToStyle_Fwd()
On Error GoTo ErrorHandler
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("chrAComms Char")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
ErrorHandler:
End Sub
Sub aaai_JumpToStyle_Back()
On Error GoTo ErrorHandler
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("chrAComms Char")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = False
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
ErrorHandler:
End Sub


Many thanks,

OT
CERTIFIED EXPERT
Commented:
Try changing "chrAComms Char " to just "chrAComms"

Author

Commented:
That DID it!  Thanks, Paul!!!!!!!!!!!!!!!!!!!!!!!!!!!! :)))))))))))))))))))))))))))))))))))))!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
CERTIFIED EXPERT

Commented:
Anytime OT!!