How to change the selected text to a different color in MS Word?

I have some simple .doc files that I created in Word 2007, where I changed the color of the text and used highlights to compare some similar texts. What I would like to do is change any instances of green text or gray highlighting to different corresponding colors for each.

I am sure there is an easy way to do this with VBA, but any other answers are also welcome.

EDIT: Although I really appreciate the answers, one that allows me to store .doc files as .docs is preferred.

+3
source share
4 answers

2007 , . (wdBrightGreen) .

Sub ChangeColor
Options.DefaultHighlightColorIndex = wdBrightGreen

    Selection.Find.ClearFormatting
    Selection.Find.Highlight = True
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Highlight = True
    Selection.Find.Execute Replace:=wdReplaceAll

    Selection.Find.ClearFormatting
    Selection.Find.Font.Color = wdColorBrightGreen
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Font.Color = wdColorRed
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
+2

HTML .

<span style='color:red'>...

<span style='background:yellow;mso-highlight:yellow'>...

, .

: , , .doc.

+1

, , " " "". . , .

+1

:

Sub RehiliteAll()

    Const YOUR_REQUIRED_COLOR_IDX As Integer = 6 'RED'
    Dim doc As Range
    Set doc = ActiveDocument.Range

    With doc.Find
        .ClearFormatting 'resets default search options'
        .Highlight = True
        .Wrap = wdFindStop

        While .Execute

            If doc.HighlightColorIndex = YOUR_REQUIRED_COLOR_IDX Then
                doc.Select
                MsgBox doc.HighlightColorIndex
                'Do stuff here'
            End If

            'doc has been reassigned to the matching'
            'range; we do this so word keeps searching'
            'forward'
            doc.Collapse wdCollapseEnd
        Wend
    End With

    Set doc = Nothing
End Sub

'I am closing comment quotes so that SO formatting'
'does not get messed up too much.'
0

Source: https://habr.com/ru/post/1698638/


All Articles