2016-11-04 43 views
0

我有一個要求,它應該計數不。句式明確的話。如果不。句子中的單詞超過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 

回答

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 
+0

我使用了Document_Close()事件並將該事件存儲在模板中。然後我製作了Template Global。這不是全球工作。 –

+0

當我關閉Word文檔時,它不會爲我啓動Document_Close()。 –

+0

嘗試使用Private Sub Document_Close()而不是Sub Document_Close(),或者從visualBasic編輯器頂部的組合框中選擇此過程,可能會有所幫助。 –