The following handler is passed the text string to search and the number of paragraphs to be deleted.
It works fine for the first pass. I need to call it repeatedly and that doesn’t work. The selection point does not move after the first time and just deletes the specified number of paragraphs from the place the first pass finished.
I need to reinitialize the selection to its default state so subsequent calls work properly.
Does someone know how to reinitialize or deselect the selection point so it will move to the next find?
on FindDelete(FindText, NumParagraphs)
set AllParagraphs to false
set ParagraphNum to NumParagraphs
tell application “Microsoft Word”
set findRange to find object of selection
clear formatting findRange – clear any previous formatting used in a find operation
set forward of findRange to true – find forward
set foundIt to false --initialize found indicator
set ParagraphNum to 1
repeat while foundIt is false
--this locates the text
tell findRange
set foundIt to execute find find text FindText -- do the search true-found false-not found
end tell
repeat while AllParagraphs is false
if ParagraphNum = NumParagraphs then
set AllParagraphs to true --this is the last paragraph to delete
end if
--the "finding" process apparently moves focus to the rest of the document
--limit to paragraph 1
delete (text object of paragraph 1 of selection) -- delete it
set ParagraphNum to ParagraphNum + 1
end repeat
end repeat
end tell
end FindDelete