Forum Discussion

roshanbangera's avatar
roshanbangera
Copper Contributor
Feb 14, 2024
Solved

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?
  • Doug_Robbins_Word_MVP's avatar
    Doug_Robbins_Word_MVP
    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.

Resources