Using Find to Highlight words in a data array

M

MsMinxy

Hi,

I am currently using the following code to search for and highlight any of a
list of words in a Word document:

Sub ReplaceList()
Dim vFindText As Variant
Dim sReplText As String
Dim sHighlight As String
Dim i As Long

sHighlight = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdBrightGreen
vFindText = Array("Word1, "Word2", "Word3", "Word4", "Word5")
sReplText = ""
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = True
For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = sReplText
.Replacement.Highlight = True
.Execute replace:=wdReplaceAll
Next i
End With
Options.DefaultHighlightColorIndex = sHighlight
End Sub

It words fine up to about 50 or so words, but I now have a list of several
hundred words and the array function will only allow you to type so many
characters within it before it breaks the line off and doesn't goes red. Is
there another way I can do this - i.e. have several consecutive arrays - or
specify the array in some other way which will allow an unlimited number of
characters and hence entries?

Any help, insight or suggestions would be greatfully appreciated!
X

MsMinxy
 
M

MsMinxy

Thanks for that Greg,

That would certainly work wouldn't it... Although the VBA used in this is
quite complicated!!! I (perhaps lazily) was hoping that there would be a
quick fix of some kind... However, if I don't find one, I can always put my
nose to the page and work out from this how to use an externally specified
array/ list/ database (whatever it is classed as).

Thanks again,

MsMinxy

Greg said:
This doesn't answer your specific question, but perhaps you will find
the following method useful:

http://gregmaxey.mvps.org/VBA_Find_And_Replace.htm
[quoted text clipped - 41 lines]
 
G

Greg Maxey

You can use a simple word table as your word list. E.g., create a
single column table in a document (call it C:\WordList.doc) and list
the words you want to find. Close the file and then run this code in
your document:

Sub ReplaceList()
Dim vFindText() As Variant
Dim sReplText As String
Dim sHighlight As String
Dim i As Long
Dim WordList As Document
Dim pTable As Word.Table
Dim j As Long
Dim oRow As Row

sHighlight = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdBrightGreen
Set WordList = Documents.Open(FileName:="C:\WordList.doc")
Set pTable = WordList.Tables(1)
'Purge any empty rows
For Each oRow In pTable.Rows
If Len(oRow.Cells(1).Range.Text) = 2 Then
oRow.Delete
End If
Next
'Size the array
ReDim vFindText(pTable.Rows.Count)
'Add the find terms to the array
For j = 1 To pTable.Rows.Count
vFindText(j) = Left(pTable.Cell(j, 1), _
Len(pTable.Cell(j, 1).Range.Text) - 2)
Next
WordList.Close SaveChanges:=wdDoNotSaveChanges

sReplText = ""
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = True
For i = 1 To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = sReplText
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
Next i
End With
Options.DefaultHighlightColorIndex = sHighlight
End Sub





Thanks for that Greg,

That would certainly work wouldn't it... Although the VBA used in this is
quite complicated!!! I (perhaps lazily) was hoping that there would be a
quick fix of some kind... However, if I don't find one, I can always put my
nose to the page and work out from this how to use an externally specified
array/ list/ database (whatever it is classed as).

Thanks again,

MsMinxy

Greg said:
This doesn't answer your specific question, but perhaps you will find
the following method useful:

http://gregmaxey.mvps.org/VBA_Find_And_Replace.htm
[quoted text clipped - 41 lines]
 
M

MsMinxy

Thanks for this Greg!

I'll try this baby out.

X
Minxy

Greg said:
You can use a simple word table as your word list. E.g., create a
single column table in a document (call it C:\WordList.doc) and list
the words you want to find. Close the file and then run this code in
your document:

Sub ReplaceList()
Dim vFindText() As Variant
Dim sReplText As String
Dim sHighlight As String
Dim i As Long
Dim WordList As Document
Dim pTable As Word.Table
Dim j As Long
Dim oRow As Row

sHighlight = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdBrightGreen
Set WordList = Documents.Open(FileName:="C:\WordList.doc")
Set pTable = WordList.Tables(1)
'Purge any empty rows
For Each oRow In pTable.Rows
If Len(oRow.Cells(1).Range.Text) = 2 Then
oRow.Delete
End If
Next
'Size the array
ReDim vFindText(pTable.Rows.Count)
'Add the find terms to the array
For j = 1 To pTable.Rows.Count
vFindText(j) = Left(pTable.Cell(j, 1), _
Len(pTable.Cell(j, 1).Range.Text) - 2)
Next
WordList.Close SaveChanges:=wdDoNotSaveChanges

sReplText = ""
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = True
For i = 1 To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = sReplText
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
Next i
End With
Options.DefaultHighlightColorIndex = sHighlight
End Sub
Thanks for that Greg,
[quoted text clipped - 18 lines]
 

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