Search Word document based on text in Excel cell.
Hi Ronald,
Am Wed, 19 Oct 2016 12:51:32 -0700 (PDT) schrieb Ronald:
Excel:
A list of cells (in one column) with keywords, say: green dog, big cat, small bird, green tree etc.
Word:
A portion of text where the spoken keywords may appear randomly.
Desired:
If a keyword (actually sentence because it can have multiple words) is found in the Word text it should be highlighted (preferrable a different color for keyword). Would be superduper if a cell in Excel near to the keyword would display if the keyword was found or not.
open your Word doc. In Excel your keywords are in column A. Then try:
Sub Test()
Dim LRow As Long, i As Long, n As Long
Dim varText As Variant
Dim myDoc As Object
With ActiveSheet
LRow = .Cells(.Rows.Count, 1).End(xlUp).Row
varText = .Range("A1:A" & LRow)
End With
Set myDoc = GetObject(, "Word.Application")
n = 4
For i = LBound(varText) To UBound(varText)
n = n + 1
With ActiveDocument.Content.Find
Options.DefaultHighlightColorIndex = n
With .Replacement
.Highlight = True
End With
.Execute FindText:=varText(i, 1), ReplaceWith:="", Format:=True,
Replace:=wdReplaceAll
End With
Next
End Sub
Set a reference to Microsoft Word xx.0 Object Library.
Regards
Claus B.
--
Windows10
Office 2016
|