View Single Post
 
Old 11-24-2014, 08:54 AM
Sandhya Sandhya is offline Windows 7 32bit Office 2007
Novice
 
Join Date: Nov 2014
Posts: 21
Sandhya is on a distinguished road
Default search for whole word means should check for before and after space

Dear All,

As below code i am able to search for the words existing in the table, but it is not checking as a whole in active document.

example: if i keep the abbrevations like "ar" means "as required" and "in" means inch, where as it is highlight all ar letters where ever it is finding,

from the word argument it is highlighting ar and in from instructions first two letters,

Kindly help me in this regard.


Quote:
Originally Posted by gmayor View Post
Create a document with a single column table. Put one of the phrases in each cell (no blanks) save the document then put the name and path in the following macro in place of "C:\Path\Table.docx". Run the macro on your document with the phrases.

Code:
Sub Highlighting()
Dim oChanges As Document, oDoc As Document
Dim oTable As Table
Dim oCell As Cell
Dim oRng As Range
Dim rFindText As Range
Const sFname As String = "C:\Path\Table.docx"
Set oDoc = ActiveDocument
Set oChanges = Documents.Open(Filename:=sFname, Visible:=False)
Set oTable = oChanges.Tables(1)
For Each oCell In oTable.Range.Cells
If Len(oCell.Range) > 2 Then
Set oRng = oDoc.Range
Set rFindText = oCell.Range
rFindText.End = rFindText.End - 1
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(FindText:=rFindText, _
MatchWholeWord:=True) = True
oRng.HighlightColorIndex = wdBrightGreen
Loop
End With
End If
Next oCell
lbl_Exit:
oChanges.Close 0
Set oChanges = Nothing
Set oDoc = Nothing
Set oRng = Nothing
Set rFindText = Nothing
Exit Sub
End Sub
Reply With Quote