Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Faster Find and Replace in MS-Word?

Status
Not open for further replies.

BruceG

Programmer
Dec 9, 2001
31
US
I am processing a large (400+ page) MS-Word document with my VB(A) program. I have various tags for client name, client address, etc. I have code that works, but it runs sloooooooow (when I was working with smaller documents, I did not notice a speed issue). I am looking for suggestions on how to make this run faster. I am using code like the following:

With mobjWord.Selection.Find
.Text = &quot;<<CLIENT_NAME>>&quot;
.Replacement.Text = &quot;John J. Doe&quot;
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With

Any suggestions on alternative methods or ways to speed this up?
 
This is from excel but I think it is common across all M$ apps

1st line:
application.screenupdating = false

last line:
application.screenupdating = true Rgds
Geoff

Vah! Denuone Latine loquebar? Me ineptum. Interdum modo elabitur
 
Thank you. I had put that in before and was able to shave some time off, but not enough to make a difference. The entire process was taking about 20 minutes (I was doing other things besides search and replace) and was able to get down to about 16 minutes by using the ScreenUpdating = False, turning off spelling and grammar checking, and using optimized table methods. However, the bottleneck still seems to be the search and replace.

Any other recommendations?
 
Word is a slow beast... How many find and replace operations like the one you describe are in your code?
Rob
[flowerface]
 
I would say several hundred. The client name is all over the place, and there are also addresses, dates, and other info, spread across all of these pages.

One thing I tried was to go &quot;outside the box&quot; and read the file as &quot;binary&quot; in straight VB, do the replacements in VB, and then write the file back out. This is lightning fast, but unfortunately, Word can't open the doc when I'm done - unless the replacement text is exactly the same length as my tags - not acceptable. The reason (upon further research) is that Word internally stores the byte count, word count, etc. of the doc - so if any of that is out of sync, Word freaks.

But - does anyone know if there is a way to have Word DEFER updating this &quot;summary information&quot; until a certain point (such as after I'm all done the replacements, instead of doing it one replacement at a time, as I assume it's doing)???
 
If you are not too concerned with retaining formatting (font and para styles, that is) you might want to consider Regular Expressions.
 
What I meant was not how many items to be replaced, but how many separate find/replace operations. It sounds like there are just a few, in which case it doesn't matter. If there were very many, I'd suggest searching just for your &quot;<<&quot; common to all searches, so you're only traversing the document once. Probably not a useful suggestion, given your case. Rob
[flowerface]
 
>How would Regular Expressions help?

Oh...perhaps because they are dramatically faster at seearching and replacing a block of text than Word itself is...
 
strongm,
That was an honest question. I wasn't sure how to use Regular Expressions in the context of my problem. However, that go me thinking, I could probably select all the text in the doc and use either reg exps or VB's native Replace function, both of which should be much faster than Word's methods. I'll try that and post back here.
 
The last thing I mentioned did not work well, because when you do text replacement on a selected portion of text like that i.e.
Selection.Text = Replace(Selection.Text, &quot;<<NAME>>&quot;, &quot;John Doe&quot;)
the formatting is not retained. This is what strongm was referring to a couple of posts ago.

Let me ask this:
Does anyone know how to (with VBA code) force Word to limit the area on which it will perform a global search and replace (rather than having it traverse the whole document, as it seems to do by default)?

 
Just a comment: I asked a question on Tek-Tips once about a
Code:
.Find
I'm doing in and xl report generator. I found that if the xl window has the focus the
Code:
.Find
is A LOT faster than if xl is
Code:
.Find
-ing in the background. I tested the xl app with and without focus; run-times were less than 1 hr WITH focus, more than 3 hrs WITHOUT focus
Possibly the Word .Find being slow caused by something similar. Might be worth taking a look at

As for narrowing down a Word search: isn't that just a case of selecting a portion of text, then do a
Code:
Selection.Find
with
Code:
WordWrap
set to
Code:
WdFindStop
(it's a long time since I used this, so please check the constant name b4 using! Can't check this here at the moment)

Cheers
Nikki
 
Thanks, Nikki, I'll give your suggestions a try.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top