Reputation: 11
I am searching for specific text within a document, deleting the text and then adding a section break. I can only get this code to work for one instance. When I tried a do while loop, checking each line, Word crashed.
With Selection.Find
.Text = "INSTRUCTOROVERVIEW"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
Selection.InsertBreak Type:=wdSectionBreakNextPage
Upvotes: 1
Views: 1724
Reputation: 19367
You should show your loop-code as well.
However, setting
.Wrap = wdFindStop
will prevent the the Find from running the code indefinitely, which is probably why it is crashing. Using wdFindContinue
will cause the search to continue from the beginning of the document, over and over.
However, you should also check the result returned by Find.Execute
. It is a Boolean
value (True or False) indicating whether the Find was successful. If is not successful you should use Exit Do
or Exit For
to break out of your loop.
Upvotes: 1