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 16, 2024
roshanbangera You can only select one at a time. However what is it that you want to do with each line of text that contains that word?
roshanbangera
Feb 19, 2024Copper Contributor
Basically I wish to change the font color of the entire line in which a specific word is found. Thankyou
- 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.
- roshanbangeraFeb 19, 2024Copper ContributorAwesome! Thannks Doug_Robbins_Word_MVP