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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word Macro is really slow. How can i speed it up?

Status
Not open for further replies.

richiwatts

Technical User
Jun 21, 2002
180
GB
I have had a lot of help creating a macro in Word that searches for word matches in the current document against a word list that I have in a seperate text file. If it finds a match it underlines the word in the current document ready for me to run another macro on the underlined words. The problem is that when I first run the Macro it takes for ages to find matches. It searches through about 100 words every 3 seconds and there are over 3000 words in the word list. i have pasted the code I have at the moment below, Does anyone have any ideas of speeding this up.

Sub FindHomonyms()
Dim oRg As Range
Dim LineFromFile As String, LineString As String
Dim path As String
Dim WordArray As Variant
Dim indx As Integer, LparenPos As Integer, RparenPos As Integer

path = ThisDocument.path
Open path & Application.PathSeparator & "data.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, LineString
LparenPos = InStr(LineString, "(")
Do While LparenPos > 0
RparenPos = InStr(LparenPos, LineString, ")")
If RparenPos > 0 Then
LineString = Left(LineString, LparenPos - 2) & _
Right(LineString, Len(LineString) - RparenPos)
LparenPos = InStr(LineString, "(")
End If
Loop
LineFromFile = LineFromFile & vbTab & LineString
Loop
Close #1

WordArray = Split(LineFromFile, vbTab)

Set oRg = ActiveDocument.Range

Application.ScreenUpdating = False

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting

.Replacement.Font.Underline = wdUnderlineWavyHeavy

.Replacement.Text = &quot;^&&quot; ' <= code for &quot;found text&quot;
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

For indx = 1 To UBound(WordArray)
.Text = WordArray(indx)
.Execute Replace:=wdReplaceAll
StatusBar = UBound(WordArray) - indx
DoEvents
Next indx
End With

Application.ScreenUpdating = True
End Sub
 

Hi there
You don't state whether your txt file is formatted in a given way or if it is a block of text of 3000 words.

I notice you used an array to do the comparison and the lack of speed was in the comparison and match on reading in

My initial suggestion would be to read only words beginnig with say the first 2 letters into a comparison array - this should negate null comparisons being added to the array, then a secondary search would be a little faster.

Additionally if it is a column or two of words there may be an opportunity to define a TYPE and use that.

But the above is hypothetical until I know the layout of the text file

jo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top