richiwatts
Technical User
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, "("![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
Do While LparenPos > 0
RparenPos = InStr(LparenPos, LineString, "
"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
If RparenPos > 0 Then
LineString = Left(LineString, LparenPos - 2) & _
Right(LineString, Len(LineString) - RparenPos)
LparenPos = InStr(LineString, "("![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
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 = "^&" ' <= code for "found text"
.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
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 = "^&" ' <= code for "found text"
.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