View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Claus Busch Claus Busch is offline
external usenet poster
 
Posts: 3,872
Default Search Word document based on text in Excel cell.

Hi Ronald,

Am Thu, 20 Oct 2016 04:26:47 -0700 (PDT) schrieb Ronald:

IF possible, and just as nice to have:
-Is it possible to mark on the Excel sheet (lets say column D) if the sentence was found?
-My list of sentences was 42 items long, the highlight color ran out of options and went grey for the last ones.


try:

Sub Test()
Dim LRow As Long, i As Long, n As Long
Dim varText As Variant
Dim myDoc As Object, myText As Object

With ActiveSheet
LRow = .Cells(.Rows.Count, 1).End(xlUp).Row
varText = .Range("A1:A" & LRow)
End With

Set myDoc = GetObject(, "Word.Application")
With myDoc
.Activate
.Visible = True
Set myText = .ActiveDocument.Content
End With
n = 4
For i = LBound(varText) To UBound(varText)
With myText.Find
Options.DefaultHighlightColorIndex = n
With .Replacement
.Highlight = True
End With
.Execute FindText:=varText(i, 1), ReplaceWith:="", Format:=True, Replace:=wdReplaceAll
If .Found = True Then
ActiveSheet.Range("D" & i) = "Match found"
n = n + 1
End If
End With
Next
End Sub


Regards
Claus B.
--
Windows10
Office 2016