如何在关闭文档之前取消突出显示文本

时间:2016-11-04 06:32:16

标签: vba macros word-vba

我要求它应该算不上。句子的句子。如果不。句子中的单词超过20,应该突出它。 &安培;当用户关闭文档时。这个突出显示应该消失。下面是我使用的代码。它算不上了。单词&然后正确突出显示。但是,当用户关闭该文档时,如何取消突出显示?

Sub Count_of_words()
'
' Count Macro
'
'
    Dim s As Range
    For Each s In Selection.Sentences
        If s.Words.count > 20 Then
            With s.Font
                .Underline = wdUnderlineWavy
                .UnderlineColor = wdColorRed
            End With         
        End If
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用Document_Close()事件 - 只需将代码放入其中,然后在文档关闭之前执行 - 您可以使用原始代码,但不使用.Underline = wdUnderlineWavy和.UnderlineColor = wdColorRed使用值突出文字。事件过程必须在ThisDocument文件中,而不是在模块文件中(您可以在Project Explorer中找到它)。所以,基本上,打开ThisDocument并编写程序:

Sub Document_Close()
'
' Count Macro
'
'

Dim s As Range
For Each s In Selection.Sentences
    If s.Words.count > 20 Then
    With s.Font
                .Underline = 'put code to no underline
                .UnderlineColor = 'put code to no color
            End With


End If

Next

End Sub