Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 12-20-2020, 09:01 AM
Chopper Chopper is offline MS WORD search macro slows way down as it runs Windows 10 MS WORD search macro slows way down as it runs Office 2019
Novice
MS WORD search macro slows way down as it runs
 
Join Date: Dec 2020
Posts: 16
Chopper is on a distinguished road
Default 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.
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
MS WORD search macro slows way down as it runs Bold Button Runs Macro DRD992 Word VBA 1 11-22-2017 02:23 PM
MS WORD search macro slows way down as it runs Word slows down as file size increases johnmill@dymeroaks.net Word 3 02-05-2016 05:49 PM
MS WORD search macro slows way down as it runs Word periodically slows / freezes when typing or printing KathyReid Word 3 09-26-2015 10:49 AM
MS WORD search macro slows way down as it runs Help:word editing slows down when copying a block diagram seeker_123 Word 1 03-02-2015 09:26 AM
MS WORD search macro slows way down as it runs Word slows to a crawl. traumatiziert Word 1 04-18-2012 12:42 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:35 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft