Avatar of oaktrees
oaktrees
 asked on

Macro in Word to Search by Named Style

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
VB ScriptMicrosoft Word

Avatar of undefined
Last Comment
Flyster

8/22/2022 - Mon
Flyster

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

Open in new window

Paul
oaktrees

ASKER
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

Open in new window


Many thanks,

OT
ASKER CERTIFIED SOLUTION
Flyster

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
oaktrees

ASKER
That DID it!  Thanks, Paul!!!!!!!!!!!!!!!!!!!!!!!!!!!! :)))))))))))))))))))))))))))))))))))))!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Flyster

Anytime OT!!