计算文档中突出显示的单词 [英] counting highlighted words in a document
问题描述
我需要使用宏来计算单词文档中突出显示的(ex BrightGreen)和未突出显示的单词。
I need to count the highlighted (ex BrightGreen) and not highlighted words within a word document using a macro.
以下简单的宏可以工作,但只能在"主要文本"中使用。它不考虑脚注。我该如何解决?
The following simply macro works but only within the "main text". It doesn't consider the footnotes. How can I fix it?
此外,它似乎也将一些特殊字符视为一个单词。如果文本如下,则为例外
Also, it seems that it considers as a word also some special characters. Ex if the text is the following
它计算1个突出显示的单词,3个未突出显示
it counts 1 highlighted word and 3 not highlighted
thanx
-
For Each w In ActiveDocument.Words
 如果w.HighlightColorIndex = wdBrightGreen则为
      highlightCount = highlightCount + 1
     否则
      nothighlightCount = nothighlightCount + 1
 结束如果
下一步
For Each w In ActiveDocument.Words
If w.HighlightColorIndex = wdBrightGreen Then
highlightCount = highlightCount + 1
Else
nothighlightCount = nothighlightCount + 1
End If
Next
推荐答案
试试这个(由Greg Maxey提供):
Try this (courtesy of Greg Maxey):
Public Sub LoopThroughRanges()
Dim rngStory As Word.Range
Dim rngSentence As Word.Range
Dim rngWord As Word.Range
Dim highlightCount As Long
Dim nothighlightCount As Long
For Each rngStory In ActiveDocument.StoryRanges
Do
For Each rngSentence In rngStory.Sentences
For Each rngWord In rngSentence.Words
If rngWord.HighlightColorIndex = wdBrightGreen Then
highlightCount = highlightCount + 1
Else
nothighlightCount = nothighlightCount + 1
End If
Next rngWord
Next rngSentence
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next rngStory
Debug.Print "Highlighted: " & highlightCount & ", not highlighted: " & nothighlightCount
End Sub
这篇关于计算文档中突出显示的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!