MS WORD search macro slows way down as it runs
I have had a fair amount of experience using VBA with Excel but recently I had to find a way to search large (several thousand lines) MS Word documents to pull out certain lines of text represented needed data - this was essentially my baptism in Word/VBA. Developing the search/identify process was relatively straightforward; it turned out that each line in the document could be treated as a paragraph (after a little judicious "pre-processing"), so I simply cycled through one paragraph at a time, checking for the desired text and outputting it when it was found - then on to the next line/paragraph. But then I ran into problems - cycling through the Word document, the macro ran slower and slower as it progressed until within a few minutes it ... just ... nearly ... stopped.
Skipping all the gnashing of teeth and rending of garments, what I eventually discovered is this:
Background - macro cycles through the document, one paragraph/line at a time, and when a desired piece of text is found the macro "if...thens" to code that handles the outputting/saving of the said text, then returns to the main program body to continue the cycling and searching process. A simple counter is used to keep track of where we are in the document, i.e., whenever a paragraph/line of text is processed, the counter is incremented; termination is achieved using a conditional after each cycle.
The Rub - apparently, in order to return to the correct position (as indicated by the above-mentioned counter) after jumping to the outputting/saving, VBA has to count from the beginning of the document after each output/save event!! Even though the counter tells it the location to return to, it has to count from the beginning to find that location. So ... as we progress farther and farther into the document, it takes longer and longer to resume the searching process. A document of a few dozen lines, no problem. Document of several thousand lines, BIG problem.
The fix was fairly simple after I finally figured out what was going on - I simply deleted each paragraph after it was examined. This means that the macro is always returning to paragraph #1 - no counting necessary. Of course this is analogous to "destructive testing" in that the original document ceases to exist after the macro runs, so keeping a master backup copy is essential.
Wondering if anyone else has encountered this? Have I misinterpreted what is going on? If so, I would be very interested in hearing about it. I have not posted my code since the problem doesn't appear (at least to me) to be code-dependent; it just seems like this is the way VBA behaves at a micro level. BTW, I did try defining the output/save code as a function and calling it that way, but the problem remained.
Any elucidative comments welcomed, and if this is an inappropriate query/comment I apologize in advance.
|