jish
02-13-2015, 11:13 AM
Would be grateful for help modifying the following macro (modified slightly from an original macro provided in a separate post by, I believe, "gmayor"---my gratitude to Mr. Mayor and my apologies for not appending this request to that original message. I've just spent 1/2 hour trying to locate that original posting but no luck...sigh.)
(1) Basically, this macro finds all occurrences of each word from a separate table file and highlights the word in yellow. I would like it to highlight just the first occurrence of the word, highlight it in yellow, and then start all over with the next word listed in the table.
(2) As icing on the cake, I would like to be able to segregate the table into sections, and have the first instance of each word from the first section of the table highlighted in yellow, the first occurrence of each word from the second section highlighted in green, those from the third section highlighted in blue, and so forth.
With thanks again for any help you can give, and special thanks to Mr. Mayor for his original posting. This is my first posting, so I hope I've done it correctly. Here's the macro:
Sub ReplaceFromTableList()
Dim oChanges As Document, oDoc As Document
Dim oTable As Table
Dim oRng As Range
Dim rFindText As Range
Dim i As Long
Dim sFname As String
Dim sAsk As String
sFname = "C:\Desktop\Test_list.doc"
Set oDoc = ActiveDocument
Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)
Set oTable = oChanges.Tables(1)
For i = 1 To oTable.Rows.Count
Set oRng = oDoc.Range
Set rFindText = oTable.Cell(i, 1).Range
rFindText.End = rFindText.End - 1
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(FindText:=rFindText, _
MatchWholeWord:=True, _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindStop) = True
oRng.HighlightColorIndex = wdYellow
oRng.Collapse wdCollapseEnd
Loop
End With
Next i
oChanges.Close wdDoNotSaveChanges
End Sub
(1) Basically, this macro finds all occurrences of each word from a separate table file and highlights the word in yellow. I would like it to highlight just the first occurrence of the word, highlight it in yellow, and then start all over with the next word listed in the table.
(2) As icing on the cake, I would like to be able to segregate the table into sections, and have the first instance of each word from the first section of the table highlighted in yellow, the first occurrence of each word from the second section highlighted in green, those from the third section highlighted in blue, and so forth.
With thanks again for any help you can give, and special thanks to Mr. Mayor for his original posting. This is my first posting, so I hope I've done it correctly. Here's the macro:
Sub ReplaceFromTableList()
Dim oChanges As Document, oDoc As Document
Dim oTable As Table
Dim oRng As Range
Dim rFindText As Range
Dim i As Long
Dim sFname As String
Dim sAsk As String
sFname = "C:\Desktop\Test_list.doc"
Set oDoc = ActiveDocument
Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)
Set oTable = oChanges.Tables(1)
For i = 1 To oTable.Rows.Count
Set oRng = oDoc.Range
Set rFindText = oTable.Cell(i, 1).Range
rFindText.End = rFindText.End - 1
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(FindText:=rFindText, _
MatchWholeWord:=True, _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindStop) = True
oRng.HighlightColorIndex = wdYellow
oRng.Collapse wdCollapseEnd
Loop
End With
Next i
oChanges.Close wdDoNotSaveChanges
End Sub