清理突出显示工具

问题描述 投票:0回答:1

因此,我编写了一个宏脚本来迭代文档的每个句子,根据字数突出显示它,并记录计数,所以最后,我会得到一个弹出窗口,准确地告诉我每个单词中有多少个句子数桶。它与我在海明威上看到的类似,但我只是想直观地看到文档中的句子组合,而不是其他语法内容或任何内容。

问题:它对我来说有点太多了。它不是简单地突出显示句子并以适当的标点符号结尾,而是突出显示整个背景单元格(借用 Excel 术语)

有人有任何建议如何解决这个问题吗?

这是我的脚本:


    Dim iCountA As Integer

    Dim iCountB As Integer

    Dim iCountC As Integer

    Dim iCountD As Integer

    Dim iCountE As Integer

    Dim iCountF As Integer

    'Saves your doc

    If Not ActiveDocument.Saved Then

        ActiveDocument.Save

    End If

    'Reset counter

    iMyCountA = 0

    iMyCountB = 0

    iMyCountC = 0

    iMyCountD = 0

    iMyCountE = 0

    iMyCountF = 0

    For Each MySent In ActiveDocument.Sentences

        If MySent.Words.Count = 0 Then

            MySent.Shading.ForegroundPatternColor = wdColorWhite

        ElseIf MySent.Words.Count < 6 Then

            MySent.Shading.ForegroundPatternColor = RGB(217, 151, 149)

            iMyCountA = iMyCountA + 1

        ElseIf MySent.Words.Count < 11 Then

            MySent.Shading.ForegroundPatternColor = RGB(250, 192, 144)

            iMyCountB = iMyCountB + 1

        ElseIf MySent.Words.Count < 16 Then

            MySent.Shading.ForegroundPatternColor = RGB(194, 214, 154)

            iMyCountC = iMyCountC + 1

        ElseIf MySent.Words.Count < 21 Then

            MySent.Shading.ForegroundPatternColor = RGB(184, 204, 228)

            iMyCountD = iMyCountD + 1

        ElseIf MySent.Words.Count < 31 Then

            MySent.Shading.ForegroundPatternColor = RGB(141, 180, 227)

            iMyCountE = iMyCountE + 1

        ElseIf MySent.Words.Count < 41 Then

            MySent.Shading.ForegroundPatternColor = RGB(178, 161, 199)

            iMyCountF = iMyCountF + 1

        End If

    Next

    MsgBox iMyCountA & " sentences shorter than or equal to 5 words." & vbCrLf & _

      iMyCountB & " sentences shorter than or equal to 10 words." & vbCrLf & _

      iMyCountC & " sentences shorter than or equal to 15 words." & vbCrLf & _

      iMyCountD & " sentences shorter than or equal to 20 words." & vbCrLf & _

      iMyCountE & " sentences shorter than or equal to 30 words." & vbCrLf & _

      iMyCountF & " sentences shorter than or equal to 40 words." & vbCrLf

End Sub

这是我第一次在宏中编码,唯一对我有用的突出显示功能是“Shading.ForegroundPatternColor”

我也尝试过:

“Shading.BackgroundPatternColor”(产生与上面相同的结果)

“Range.HighlightColorIndex”(收到 438 错误)

“替换.突出显示”(收到 438 错误)

vba ms-word
1个回答
0
投票

您的代码即将完成。

  • 添加了从最后一句中删除段落标记的代码
  • Option Explicit
    对于避免编码错误很有用。例如。
    Dim iCountA As Integer
    iMyCountA = iMyCountA + 1
  • 中使用了不同的变量
Option Explicit
Sub demo()
    Dim iCountA As Long, iCountB As Long, iCountC As Long
    Dim iCountD As Long, iCountE As Long, iCountF As Long
    Dim iColor As Long, iCount As Long
    Dim mySent As Range
    'Saves your doc
    If Not ActiveDocument.Saved Then
        ActiveDocument.Save
    End If
    'Reset counter
    iCountA = 0
    iCountB = 0
    iCountC = 0
    iCountD = 0
    iCountE = 0
    iCountF = 0
    For Each mySent In ActiveDocument.Sentences
        ' Remove the paragraph marker from the last sentence
        If mySent.Paragraphs(1).Range.End = mySent.End Then
            mySent.End = mySent.End - 1
        End If
        iColor = 0
        iCount = mySent.Words.Count
        If iCount = 0 Then
            iColor = wdColorWhite
        ElseIf iCount < 6 Then
            iColor = RGB(217, 151, 149)
            iCountA = iCountA + 1
        ElseIf iCount < 11 Then
            iColor = RGB(250, 192, 144)
            iCountB = iCountB + 1
        ElseIf iCount < 16 Then
            iColor = RGB(194, 214, 154)
            iCountC = iCountC + 1
        ElseIf iCount < 21 Then
            iColor = RGB(184, 204, 228)
            iCountD = iCountD + 1
        ElseIf iCount < 31 Then
            iColor = RGB(141, 180, 227)
            iCountE = iCountE + 1
        ElseIf iCount < 41 Then
            iColor = RGB(178, 161, 199)
            iCountF = iCountF + 1
        End If
        If iColor > 0 Then mySent.Shading.ForegroundPatternColor = iColor
    Next
    MsgBox iCountA & " sentences shorter than or equal to 5 words." & vbCrLf & _
      iCountB & " sentences shorter than or equal to 10 words." & vbCrLf & _
      iCountC & " sentences shorter than or equal to 15 words." & vbCrLf & _
      iCountD & " sentences shorter than or equal to 20 words." & vbCrLf & _
      iCountE & " sentences shorter than or equal to 30 words." & vbCrLf & _
      iCountF & " sentences shorter than or equal to 40 words." & vbCrLf
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.