【发布时间】:2015-10-09 05:30:43
【问题描述】:
下面的宏应该提取每个句子的平均单词,然后将所有句子中>=150% 的单词变成红色。
问题是,它也会将 一些 较短的句子变成红色。例如,它为这些句子着色(编辑添加:在源文档中,150% 的平均长度是 35 个单词):
31 个字:FSAIP 为基于此假设的操作过程评估设计的监管实施的充分性提供了基础,并支持准备前瞻性剂量评估。
29 字:(根据 10 CFR 835.2,等效剂量率标准适用于距辐射源 30 厘米或距辐射穿透的任何表面 30 厘米。)
(我会分享更多示例,但这是联邦核项目的辐射控制程序,因此我必须谨慎选择。)
以上句子的字数来自窗口底部的状态栏。所以 Word 似乎在计算单词的数量,具体取决于 Word 的计数部分。我认为。
对于如何使计数更准确,或者至少在两种情况下都相同,有什么建议吗?哦,最后一点:它不包括可见的已删除单词。在某些情况下,它可能会计算诸如不间断连字符之类的东西,但在上面共享的那些中却没有。
Sub Mark_Long()
'''''''''''''''''''
' Adapted from "Allen Wyatt's Word Tips, wordribbon.tips.net.
' I added to it so it pulls the avg sentence length from
' the readability stats, and only marks the sentences that are 150%
' of the average.
''''''''''''''''''''
Dim iMyCount As Integer
Dim iWords As Integer
Dim bTrackingAsWas As Boolean
If Not ActiveDocument.Saved Then
ActiveDocument.Save
End If
Set myRange = ActiveDocument.Content
wordval = myRange.ReadabilityStatistics(6).Value
bTrackingAsWas = ActiveDocument.TrackRevisions
'Turn off tracked changes
ActiveDocument.TrackRevisions = False
'Reset counter
iMyCount = 0
'Set number of words
iWords = (wordval * 1.5)
For Each MySent In ActiveDocument.Sentences
If MySent.Words.Count > iWords Then
MySent.Font.Color = wdColorRed
iMyCount = iMyCount + 1
End If
Next
'Restore tracked changes
ActiveDocument.TrackRevisions = bTrackingAsWas
'Report results
MsgBox iMyCount & " sentences longer than " & _
iWords & " words."
End Sub
【问题讨论】:
-
我只是花了一些时间试图重现他的问题。确实,VBA 字数与 Word GUI 中的字数存在差异。大多数情况下,它会添加一个单词,但我似乎有些情况下它已经偏离了近 5 个。到目前为止还没有找到解释
标签: vba ms-word word-count