vba 将突出显示的文本更改为不同的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/229865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:27:01  来源:igfitidea点击:

Changing highlighted text to a different color

vbams-wordword-vbahighlightingword-2007

提问by Eugene M

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

我有一些在 Word 2007 中创建的简单 .doc 文件,我在其中更改了文本颜色并使用突出显示来比较一些相似的文本。我想要做的是将绿色文本或灰色突出显示的任何实例更改为各自不同的颜色。

I'm sure there is a simple way to do this with VBA but any other sort of answers are also welcome.

我确信有一种简单的方法可以用 VBA 做到这一点,但也欢迎任何其他类型的答案。

EDIT: While I do appreciate answers, one that allows me to keep the .doc files as .docs is preferred.

编辑:虽然我很欣赏答案,但首选允许我将 .doc 文件保留为 .docs 的答案。

回答by Fionnuala

This is not from 2007, but the idea should suit. This example changes any current highlight to the new default highlight (wdBrightGreen) and any green text to red.

这不是 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

回答by buti-oxa

You can always save the file as HTML, and replace colors there. Color is represented with

您始终可以将文件另存为 HTML,并在那里替换颜色。颜色表示为

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

and highlight is

重点是

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

Should be easy to manipulate if your document is simple enough.

如果您的文档足够简单,应该很容易操作。

Edit that answers the edit in the question: After you are done, re-open the file and save the file back as .doc.

编辑回答问题中的编辑:完成后,重新打开文件并将文件另存为 .doc。

回答by Bruce

I thought one could highlight a section of the coloured text and then choose "select text with similar formatting" option from the select text menu option on the Home tab. Then just select the text colour required. Hope this works.

我认为可以突出显示彩色文本的一部分,然后从“主页”选项卡上的“选择文本”菜单选项中选择“选择具有相似格式的文本”选项。然后只需选择所需的文本颜色。希望这有效。

回答by guillermooo

This should work for your purpose:

这应该适用于您的目的:

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.'