1

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

但是,结果与 Word 中的 Find 函数返回的结果相差很大,有人知道为什么会出现这种差异吗?我是在重复计算吗?

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
4

1 回答 1

3

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

highlightCount = highlightCount + rngWords.Words.Count

highlightCount = highlightCount + rngWords.ComputeStatistics(wdStatisticWords)

他们会匹配。

于 2013-03-16T16:34:37.647 回答