Link to home
Start Free TrialLog in
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
Avatar of Flyster
Flyster
Flag of United States of America image

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
Avatar of oaktrees
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
Avatar of Flyster
Flyster
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
That DID it!  Thanks, Paul!!!!!!!!!!!!!!!!!!!!!!!!!!!! :)))))))))))))))))))))))))))))))))))))!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Anytime OT!!