I am writing a macro in MS Word which should find all highlighted text in a document and perform some action on each. I am planning a loop to do the search and manipulation part and have no problem with this portion of the code.
But I don’t know how to find how many iterations I’m going to need. Is there a way to determine the number of highlights in VBA?
Many thanks in advance.
I am writing a macro in MS Word which should find all highlighted text
Share
There’s no need to calculate the number of matches up-front. You can execute the search in a loop and it will stop once there are no more matches.
Make sure you apply the search to the right part of the document.I used
ActiveDocument.Range, but anyRangeobject will do. Maybe something more specific is better for your case.Also, check out the many other properties of the
Findobject and set them to sensible values, this is better than going with the defaults (nobody can remember all defaults for all options, plus theFindobject might already be set up by some earlier search).