Loop Find Text with MsgBox

P

Petra Liverani

I would like to loop a find text and produce MsgBox but after the code has
run, it switches to the macro window and doesn't run any futher code. I
tried putting in another loop relating to the answer (vbOK) but I couldn't
get a sense of the logic. This is what I've got so far.

Sub FindMsgBox()

With Selection.Find
Do While .Execute(FindText:="assessment task", Wrap:=wdFindStop,
Forward:=True) = True
MsgBox ("Delete any references")
Loop
End With
End Sub

Thanks for any help.
Petra
 
J

Jay Freedman

Hi, Petra,

Your code works exactly as I would expect. In a sample document where the
text "assessment task" appears in three places, the macro selects each of
them and displays the message box, then goes on to the next one. After the
third message box, the macro ends. What else did you expect it to do?
 
P

Petra Liverani

Hi Jay,

Thank you for your reply. I'm sorry, my question was not clear - and I'm
supposed to be a technical writer. I won't post questions again when my
brain is in the state it was.

The first MsgBox loop routine works OK but none of the following MsgBox
loops do. However, further code asking the user if they want to remove
highlight does run (I hadn't added this code when I posted my message so I
thought that the code just stopped at the first MsgBox loop). Even when I
put in each loop as its own routine without calling it only the first loop
works.

'Not calling routine
With Selection.Find
Do While .Execute(FindText:="list what", Wrap:=wdFindStop,
Forward:=True) = True
MsgBox ("Delete what and rephrase")
Loop
End With

With Selection.Find
Do While .Execute(FindText:="assessment task", Wrap:=wdFindStop,
Forward:=True) = True
MsgBox ("Delete any references")
Loop
End With

'Calling routine
Call DoFindMsgBox("state what", "Delete what and rephrase")
Call DoFindMsgBox("describe what", "Delete what and rephrase")

'Confirm remove highlight
Selection.HomeKey wdStory
sAnswer = MsgBox("Do you want to remove highlight?", vbYesNo, "Remove
Highlight")
If sAnswer = vbYes Then
Selection.WholeStory
Options.DefaultHighlightColorIndex = wdNoHighlight
Selection.Range.HighlightColorIndex = wdNoHighlight
 
J

Jay Freedman

Hi, Petra,

I'm still not completely clear on what you're doing with this macro, but if
it's what I suspect -- search through the document for the phrase "list
what", then search through it again for the phrase "assessment task", and
finally ask the user whether to remove all the highlighting in the
document -- then you're missing one line.

When the first loop searches the document, it leaves the Selection on the
final occurrence of "list what". The search for "assessment task" begins at
that point, *not* from the beginning of the document. If there are no
occurrences of "assessment task" after the last occurrence of "list what",
then the search doesn't go anywhere, so it looks like it isn't executing.

To fix this, make a copy of the line

Selection.HomeKey wdStory

and paste it after the first "End With". Now the search for "assessment
task" will start at the top of the document.

In fact, if it's possible that the macro will start running while the cursor
is somewhere below the top of the document, you might want to paste another
copy of the same line before the first loop.
 
P

Petra Liverani

Hi Jay,

Thank you so much for your reply. What a basic mistake. Making this macro
work is going to make the proofing process so much quicker. I'm still not
sure why it didn't work on my test document without that line. Because the
first instance of "list what" is after the last instance of "assessment
task". But it needed to be in there even if the macro did work without it.

Thanks again.
Petra
 

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