Word 2010 VBA: Search for text and replace with section break -


i searching specific text within document, deleting text , adding section break. can code work 1 instance. when tried 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 selection.find.execute selection     if .find.forward = true         .collapse direction:=wdcollapsestart     else         .collapse direction:=wdcollapseend     end if     .find.execute replace:=wdreplaceone     if .find.forward = true         .collapse direction:=wdcollapseend     else         .collapse direction:=wdcollapsestart     end if     .find.execute end selection.insertbreak type:=wdsectionbreaknextpage 

you should show loop-code well.

however, setting

.wrap = wdfindstop 

will prevent the find running code indefinitely, why crashing. using wdfindcontinue cause search continue beginning of document, on , over.

however, should check result returned find.execute. boolean value (true or false) indicating whether find successful. if not successful should use exit do or exit for break out of loop.


Comments