How to redirect on "Not Found"??

L

Lighthouse

I have a macro that searches my document and looks for a specific
marker that may or may not exist in the document. If it finds the
marker it is supposed to do some stuff from that marker to the end of
the document.

It works great (thanks to Dave Lett) when the marker is found, but I
need to tell it to stop if the marker is not found. This is what I
have:

With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Text = "marker"
.Style = "SuperHeading"
.Replacement.Text = ""
.Execute
End With
.MoveDown Unit:=wdParagraph, Count:=1

[do some stuff here]

what's happening is that if the marker is not found it does the [do
some stuff here] steps anyway. I need a way to quit the macro if my
..Text is not found.

Thanks.
 
J

Jonathan West

Lighthouse said:
I have a macro that searches my document and looks for a specific
marker that may or may not exist in the document. If it finds the
marker it is supposed to do some stuff from that marker to the end of
the document.

It works great (thanks to Dave Lett) when the marker is found, but I
need to tell it to stop if the marker is not found. This is what I
have:

With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Text = "marker"
.Style = "SuperHeading"
.Replacement.Text = ""
.Execute
End With
.MoveDown Unit:=wdParagraph, Count:=1

[do some stuff here]

what's happening is that if the marker is not found it does the [do
some stuff here] steps anyway. I need a way to quit the macro if my
.Text is not found.

The Execute method returns a True/False value depending on the result of the
search. You can use it like this

With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Text = "marker"
.Style = "SuperHeading"
.Replacement.Text = ""
End With
If .Find.Execute
.MoveDown Unit:=wdParagraph, Count:=1

[do some stuff here]

Else

[do other stuff here

End If
End With
 
L

Lighthouse

Thanks Jonathan. That looks promising but I'm getting a "syntax error"
failure on the

If .Find.Execute

any ideas on that?
 
J

Jonathan West

Lighthouse said:
Thanks Jonathan. That looks promising but I'm getting a "syntax error"
failure on the

If .Find.Execute

any ideas on that?

Yup, a typo in my part. Should be this

If .Find.Execute Then
 
L

Lighthouse

right. i moved the "End with" part above the "if find.execute then"
piece. this syntax stuff is what frustrates me the most w/ VB. the
"help" is nearly unintelligible to someone who is not a programmer.
 

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