Forum Discussion
roshanbangera
Feb 14, 2024Copper Contributor
Selecting an entire line containing a specific word
I wished to know if there is any possible way to automatically select an entire line where a specific word occurs? If there is, how can we do it for multiple (all) occurrences as well?
- Feb 19, 2024
roshanbangera Use the following code
Dim strWord As String Dim rngFound As Range strWord = InputBox("Insert the word to be found", "Word Finder") Selection.HomeKey wdStory With Selection.Find Do While .Execute(FindText:=strWord, Forward:=True, MatchWholeWord:=True, _ MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=True) = True Set rngFound = Selection.Bookmarks("\line").Range.Duplicate Selection.Collapse wdCollapseEnd rngFound.Font.ColorIndex = wdRed Loop End WithReplace the wdRed with whatever colour you want.
Feb 19, 2024
roshanbangera Use the following code
Dim strWord As String
Dim rngFound As Range
strWord = InputBox("Insert the word to be found", "Word Finder")
Selection.HomeKey wdStory
With Selection.Find
Do While .Execute(FindText:=strWord, Forward:=True, MatchWholeWord:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=True) = True
Set rngFound = Selection.Bookmarks("\line").Range.Duplicate
Selection.Collapse wdCollapseEnd
rngFound.Font.ColorIndex = wdRed
Loop
End With
Replace the wdRed with whatever colour you want.
roshanbangera
Feb 19, 2024Copper Contributor
Awesome! Thannks Doug_Robbins_Word_MVP