Search macro

R

Rees

I need the simplest macro in the world -- to do a Find of the next instance
of Heading 1 (any character). The following, which was generated by
keystrokes and not edited, will not work. The search either does not take
place or it is unsuccessful. Can you see what might be wrong with this
code? Thanks.

Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
 
W

WCyrek

..Text = "^?"

^? is a symbol for any character

Also Im not sure you need the move right line in the
begining
 
J

Jonathan West

Hi Rees,

drop some unnecessary lines, like this

Selection.Collapse Direction:=wdCollapseEnd
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
With Selection.Find
.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Execute
End With
Selection.Collapse Direction:=wdCollapseStart
 
R

Rees

This works great. Thanks. Oddly enough, it doesn't work when I create the
flip-side macro that needs to say Forward = False, in order to search for
the previous instance of the style.
 
J

Jonathan West

Rees said:
This works great. Thanks. Oddly enough, it doesn't work when I create the
flip-side macro that needs to say Forward = False, in order to search for
the previous instance of the style.

For working backwards, change this line

Selection.Collapse Direction:=wdCollapseEnd

to this

Selection.Collapse Direction:=wdCollapseStart

and change this

.Forward = True

to this

..Forward = False
 
R

Rees

Thanks. That's what I had, but it didn't work until after a re-boot. I'm
running it from a toolbar in startup, and I thought (and still think) that
it should have worked after going out and in Word rather than not until
after a re-boot. Appreciate your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top