【问题标题】:Word Macro to colorize text in VBA formatWord 宏以 VBA 格式为文本着色
【发布时间】:2020-07-03 16:21:14
【问题描述】:

我有一个包含 VBA 代码块的 Word 文档。有没有人见过一个宏,它将通过将关键字着色为蓝色、cmets 绿色等来格式化它,使其看起来像它在 VBA 编辑器中的显示方式?

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    您可以使用此代码突出显示关键字。该代码通过搜索所选范围并更改单词颜色来工作。

    您可以在End If 代码的第一部分之后添加并更改文本。 祝你好运!

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Set myRange = Range("A1:AG100")  'The Range that contains the substring you want to change color
        substr = "t"   'The text you want to change color
        txtColor = 3   'The ColorIndex which represents the color you want to change
    
    
        For Each myString In myRange
            lenstr = Len(myString)
            lensubstr = Len(substr)
            For i = 1 To lenstr
                tempString = Mid(myString, i, lensubstr)
                If tempString = substr Then
                    myString.Characters(Start:=i, Length:=lensubstr).Font.ColorIndex = txtColor
                End If
            Next i
        Next myString
    
         Set myRange = Range("A1:AG100")  'The Range that contains the substring you want to change color
        substr = "u"   'The text you want to change color
        txtColor = 4   'The ColorIndex which represents the color you want to change
          For Each myString In myRange
            lenstr = Len(myString)
            lensubstr = Len(substr)
            For i = 1 To lenstr
                tempString = Mid(myString, i, lensubstr)
                If tempString = substr Then
                    myString.Characters(Start:=i, Length:=lensubstr).Font.ColorIndex = txtColor
                End If
            Next i
        Next myString
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-06
      • 2023-03-13
      • 2020-10-15
      • 1970-01-01
      • 2019-02-16
      • 2020-07-04
      • 2023-04-06
      • 2016-02-05
      相关资源
      最近更新 更多