通过查找功能进行字数统计返回与宏不同的结果

时间:2013-03-16 14:46:54

标签: vba ms-word ms-office

我有一个字数统计函数,用于查找突出显示或粗体和非下划线的文本,然后返回符合该条件的文档中的字数。

然而,结果与查找功能在单词中返回的结果差别很大,任何人都不知道为什么会出现差异?我在重复计算一些事情吗?

Sub CountWords()

Dim rngWords As Range
Set rngWords = ActiveDocument.Content
Dim boldCount As Long, highlightCount As Long
Dim wordTotal As Long

Do
With rngWords.Find
    .Highlight = True
    .Forward = True
    .Execute
End With
If rngWords.Find.Found = True Then
    highlightCount = highlightCount + rngWords.Words.Count
Else
    Exit Do
End If
Loop

Set rngWords = ActiveDocument.Content

Do
With rngWords.Find
    .Font.Bold = True
    .Highlight = False
    .Font.Underline = wdUnderlineNone
    .Forward = True
    .Execute
End With
If rngWords.Find.Found = True Then
    boldCount = boldCount + rngWords.Words.Count
Else
    Exit Do
End If
Loop
wordTotal = boldCount + highlightCount
MsgBox "There are " & wordTotal & " words to be spread"
End Sub

1 个答案:

答案 0 :(得分:3)

Words属性计算标点符号和段落标记。变化

highlightCount = highlightCount + rngWords.Words.Count

highlightCount = highlightCount + rngWords.ComputeStatistics(wdStatisticWords)

他们会匹配。