I use MS Word 2003. I'm trying to find and highlight words in Word docs (consisting of single words and milti-word phrases like "I think that") but can't figure out how to get the script to highlight multi-word phrases.
I tried the following two script and had the following issues:
1.
`````````````
Sub CompareWordList()
Dim sCheckDoc As String
Dim docRef As Document
Dim docCurrent As Document
Dim wrdRef As Object
sCheckDoc = "c:\checklist.doc"
Set docCurrent = Selection.Document
Set docRef = Documents.Open(sCheckDoc)
docCurrent.Activate
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Font.Bold = True
.Replacement.Text = "^&"
.Forward = True
.Format = True
.MatchWholeWord = True
.MatchCase = True
.MatchWildcards = False
End With
For Each wrdRef In docRef.Words
If Asc(Left(wrdRef, 1)) > 32 Then
With Selection.Find
.Wrap = wdFindContinue
.Text = wrdRef
.Execute Replace:=wdReplaceAll
End With
End If
Next wrdRef
docRef.Close
docCurrent.Activate
End Sub
PROBLEM: It highlights each word of my multi-word items instead of the whole thing and it highlights parts of whole words. I put all my words in my word doc seperated by spaces and quotes around the multi-word entries.
`````````````
2.
```````````````
Const wdReplaceAll = 2
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open("C:\edit.doc")
Set objSelection = objWord.Selection
arrWords = Array("I think that", "very")
For Each strWord In arrWords
objSelection.Find.Text = strWord
objSelection.Find.Forward = True
objSelection.Find.MatchWholeWord = True
objSelection.Find.Replacement.Highlight = True
objSelection.Find.Execute , , , , , , , , , , wdReplaceAll
Next
PROBLEM: It appears this script just skipped the multi-word items and didn't highlight it. It only highlighted the 2nd word.
``````````````
Whether I use a seperate file with all my words or input all the words and phrases into an array, I simply need it to recognize multi-word entries as a unit and not individual words.
Thank you for your time,
Shane