Home > Software design >  Format text from a character or word to the end of the line
Format text from a character or word to the end of the line

Time:05-28

The code below finds the text "//" and formats the entire line with green color.

I'm trying to modify it so that it's only formatted from the text "//" to the end of the line. (including "//")

So instead of this result:

example text //color changed

I aim to achieve this:

example text //color changed

Sub FindWord_and_ChangeColor()

    Dim LastRow, i As Long
    
    Selection.HomeKey Unit:=wdStory
    
    With Selection.Find
        .ClearFormatting
        .Forward = True
        .Wrap = wdFindContinue
        .Text = "//"
        .Execute
    End With
    
    Do While Selection.Find.Found
        Selection.Expand Unit:=wdSentence
        Selection.Font.Color = RGB(107, 187, 117)
        Selection.Collapse Direction:=wdCollapseEnd
        Selection.Find.Execute
    Loop
End Sub

I'm recording macros to see if I can find a solution in the auto-generated code. But I thank in advance to anyone who can help me.

CodePudding user response:

Change

Selection.Expand Unit:=wdSentence

to

Selection.MoveEnd Unit:=wdSentence, Count:=1

  • Related