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
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