ms word - VBA code to search for text and stop at next occurrence including footnotes, headers etc -
i trying write code search through stories including headers, footers, footnotes etc , stop @ each occurrence user can make decision (it may or may not change), click button again move next occurrence (like word's find next). aware there pretty tricky code performing search , replace using range object , have code working part if project, can't make search , stop @ selected text, carry on looking in different storeies, stops @ end of main document. code below looks though should work if footnote example has text searched for, ignoring it. have done thorough search of site , others , have found several examples search , replace, none search , stop/select. advice gratefully received - thank you.
sub testselection()
dim rngstory range dim docdocument document set docdocument = activedocument docdocument each rngstory in .storyranges select case rngstory.storytype case 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 debug.print rngstory.storytype selection.find .clearformatting .text = "xyz" .replacement.text = "" .forward = true .wrap = wdfindstop .format = true .matchcase = false .matchwholeword = false .matchwildcards = false .matchsoundslike = false .matchallwordforms = false end selection.find.execute if selection.find.found = true exit sub end if end select next rngstory end end sub
whether problem in case don't know, while loop iterates on ranges returned storyranges, not process entire document. includes first part of each story. (so, example, if there several sections in document, include header & footer first section).
you need use nextstoryrange method in order access entire story. in vba example loop construct. (it's horrible api - bad range.find!).
also, aware executing search change selection, selection.find searching in last result, rather entire range.
Comments
Post a Comment