【问题标题】:Changing highlighted text to a different color将突出显示的文本更改为不同的颜色
【发布时间】:2010-09-18 19:05:19
【问题描述】:

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

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

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

【问题讨论】:

    标签: vba ms-word highlighting word-2007


    【解决方案1】:

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

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

    高亮是

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

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

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

    【讨论】:

    • 实际上它有点复杂,因为 mso-highlight:something 键值对可以隐藏在其他这样的对中(关于编码、语言、字体选择等)。我的建议是在 span 元素的样式属性中寻找 mso-highlight:something。
    • mso-highlight 使用 HTML 命名的颜色。因此,例如,如果您将其更改为背景并将 mso-highlight 更改为aliceblue,它将起作用。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      这应该适合您的目的:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2016-03-29
          • 1970-01-01
          • 2019-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-29
          相关资源
          最近更新 更多