Find and replace very slow

M

MNT77

I've vrite code below to find and replace words in MSWord.
I've installed my application in a lot of PC and all works fine.
I've 2 PC with Win XP Pro and Office 200 Pro where this code run extremly
slow (minutes to replace 100 word).
When application run on this PC i see word and it is slow but if i click on
word window immedialy become fast. Help pls

The Code:
Private Sub Comando0_Click()
Dim WordApp As Word.Application
Set WordApp = New Word.Application
WordApp.Visible = True
Dim WordDoc As Word.Document
Set WordDoc = WordApp.Documents.Open("C:\TimeTest\doc2.doc")
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Dim i As Integer
For i = 0 To 100
Dim strFind As String
Dim strReplace As String
strFind = "Campo" & i + 1
strReplace = "Campo" & i + 2
With Selection.Find
.Text = strFind
.Replacement.Text = strReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next
MsgBox "OK"
End Sub
 
D

Doug Robbins - Word MVP

When i = 0

FindString = campo1 replacestring = campo2

When i = 1

FindSting = Campo2 replac string = campo3

So you are then replacing what was campo1 originally with campo3

and so on.

Instead, try using

For i = 100 to 0 step -1

That way, things will only be replaced one time
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
M

MNT77

This code is just to test time for doing find and replace.
Normally it takes 3/5 seconds to replace "Campo1" to "Campo2" to ...
"Campo100"
In case of this 2 PC this scripts take 7 minutes!!! but if i click on word's
window during process then it speed up to normally speed. I want to know why
it speed up only if i click on word's window ?
 
D

Doug Robbins - Word MVP

I cannot explain the speed increase, but re-declaring the variables each
time is not going to help.

Try the following

Dim i As Long
For i = 0 To 100
Selection.HomeKey wdStory
With Selection.Find
.Text = "Campo" & i + 1
.Replacement.Text = "Campo" & i + 2
.Execute Replace:=wdReplaceAll
End With
Next i

I am not sure whether you are starting with Campo1 just one time or 100
times, but that change doesn't make much difference here
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
M

MNT77

redeclaring vars take time but i'm talking about minutes to find and replace
a word in a simple document 100 times. And when i click (or simple move mouse
on document) speed rapidly increase to normal (3 sec to complete)

I wrote code as access module and as vb.net exe application but in all 2
enviornment problem is present... I can't find reason...
 
D

Doug Robbins - Word MVP

Here, using a document in which Campo1 appears 100 times, using that code is
almost instanteous. That's 10,000 replacements.

Of course, I am just doing it all in Word, not from another application.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top