Find Loop only works on first 3 of find.execute then stops

J

jeninOk

Can anyone please tell me why this macro works on the first three occurances
of finding strFind (of a total of 43 occurances) and then stops after the
third occurance of finding strFind with a message:

Code Execution has been interrupted.
When I click Debug, this line in the code is highlighted in Yellow:
If Not Selection.Find.Found Then Exit Do

MACRO:

Sub findandType()

Dim strFind As String
Dim strTypeTxt As String

strFind = "Site Address"
strTypeTxt = "Site City:"

Selection.HomeKey Unit:=wdStory ' move to top of document
Do ' STARTS LOOP
With Selection.Find
.Text = strFind
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop 'CRITICAL so not asked to continue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Not Selection.Find.Found Then Exit Do ' STOP IF NOT FOUND ANYMORE
'STUFF TO DO AFTER TEXT IS FOUND
Selection.MoveDown Unit:=wdLine, Count:=1 'move down one line
Selection.HomeKey Unit:=wdLine 'move to the beginning
of the line
Selection.TypeText Text:=strTypeTxt 'type this text
'find the next occurance of strFind
Loop
End Sub
 
S

StevenM

To: JeninOK,

It appears to work fine for me. I don't know why you experieced problems.

For what its worth, I also modified your macro.

Sub findandType()
Dim strFind As String
Dim strTypeTxt As String

strFind = "Site Address"
strTypeTxt = "Site City:"

Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
Do
.Text = strFind
.Forward = True
.Wrap = wdFindStop
.Execute
If Not .Found Then Exit Do
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdLine
Selection.TypeText Text:=strTypeTxt
Loop
End With
End Sub

But your original code seemed to work for me as well.

Steven Craig Miller
 
J

jeninOk

I think te problem is bigger -- all my macros -- even in Excel seem to not be
completing themselves. Is this a virus, maybe? IF so, wha's the best fix?
 
D

Doug Robbins - Word MVP

Your code should be more like
Dim myrange as Range
With ActiveDocument
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(Findtext:="SiteAddress", Forward:=True,
MatchWildcards:=False, Wrap:=wdFindStop) = True
Set myrange = Selection.Paragraphs(1).Range
myrange.End = myrange.End + 1
myrange.Collapse wdCollapse wdCollapseEnd
myrange.InsertBerfore "Site City"
Loop
End With
Source.End = .Range.End
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
T

Tony Jollans

This is a 'feature' :-(

Slowly more and more reports of this are coming in, but, as yet, there is no
known solution or workaround.

You can click on Continue - or press Alt+C - and your code will continue,
but you may have to do it several times. Once it has started happening, it
seems to continue until, at least, all Office applications have been closed
(at the same time) and then restarted, and sometimes until after rebooting.
When it does happen it affects macros in all Office applications.
 

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