使用 VBA 来计算活动文档中的字符数:
ActiveDocument.Range.ComputeStatistics(wdStatisticCharacters)
或者
Activedocument.Range.Characters.Count
要获取当前选择的计数:
Selection.Range.ComputeStatistics(wdStatisticCharacters)
或者
Selection.Range.Characters.Count
每个示例中的第二种方法将空格计为字符,第一种方法没有。
编辑:我对各种方法进行了一些速度测试,以计算文档中 char 的实例。正则表达式和将文档内容填充到字符串中是最快的 - 比循环遍历每个字符或FIND
对于我的测试文档,我将此网页的内容复制到 Word 文档中。作为准确性检查,我使用 Word 的Find
函数/面板来查找小写“a”的实例数。在我编辑这个答案之前,它是 409 个实例。
然后,我创建了四个函数来计算 Word 文档中字符(实际上是任何字符串)的实例数。第一个简单地遍历文档中的每个字符,类似于 Andrew 的。第二个使用Find
函数。第三个将文档的内容填充到一个字符串中并循环遍历它。第四个做同样的事情,但使用正则表达式检查匹配:
Function GetCharCountLoop(doc As Word.Document, char As String) As Long
Dim i As Long
Dim CharCount As Long
With doc.Content.Characters
For i = 1 To .Count
If .Item(i) = char Then
CharCount = CharCount + 1
End If
Next i
End With
GetCharCountLoop = CharCount
End Function
Function GetCharCountFind(doc As Word.Document, char As String) As Long
Dim i As Long
Dim CharCount As Long
With doc.Content.Find
Do While .Execute(FindText:=char, Forward:=True, MatchWholeWord:=False, MatchCase:=True) = True
CharCount = CharCount + 1
Loop
GetCharCountFind = CharCount
End With
End Function
Function GetCharCountString(doc As Word.Document, char As String) As Long
Dim chars As String
Dim i As Long
Dim CharCount As Long
chars = doc.Content
For i = 1 To Len(chars)
If Mid$(chars, i, 1) = char Then
CharCount = CharCount + 1
End If
Next i
GetCharCountString = CharCount
End Function
Function GetCharCountRegex(doc As Word.Document, char As String) As Long
Dim chars As String
Dim CharCount As Long
Dim objRegExp As Object
chars = doc.Content
Set objRegExp = CreateObject("VBScript.RegExp")
With objRegExp
.Pattern = char
.IgnoreCase = False
.Global = True
CharCount = .Execute(chars).Count
End With
GetCharCountRegex = CharCount
End Function
然后我使用这个子测试它们,运行一个循环:
Sub TimeMethods()
Dim char As String
Dim CharCount As Long
Dim LoopCounter As Long
Dim NumLoops As Long
Dim StartTime As Double
char = "a"
NumLoops = 1
StartTime = Timer
For LoopCounter = 1 To NumLoops
CharCount = GetCharCountLoop(ActiveDocument, char)
Next LoopCounter
Debug.Print CharCount
Debug.Print Timer - StartTime
StartTime = Timer
For LoopCounter = 1 To NumLoops
CharCount = GetCharCountFind(ActiveDocument, char)
Next LoopCounter
Debug.Print CharCount
Debug.Print Timer - StartTime
StartTime = Timer
For LoopCounter = 1 To NumLoops
CharCount = GetCharCountString(ActiveDocument, char)
Next LoopCounter
Debug.Print CharCount
Debug.Print Timer - StartTime
StartTime = Timer
For LoopCounter = 1 To NumLoops
CharCount = GetCharCountRegex(ActiveDocument, char)
Next LoopCounter
Debug.Print CharCount
Debug.Print Timer - StartTime
End Sub
结果是戏剧性的:
GetCharCountLoop - 514.3046875 秒
GetCharCountFind - 0.5859375 秒
GetCharCountString - 0.015625 秒
GetCharCountRegex - 0.015625 秒
我从运行中删除了 GetCharCountLoop,并运行了其他三个 100 次。按照这个基本的时序,将内容塞进一个字符串并计数,或者使用正则表达式,几乎比 Find 方法快 50 倍:
GetCharCountFind - 30.984375 秒
GetCharCountString - 0.6328125 秒
GetCharCountRegex - 0.578125 秒
请注意,第一种方法的速度很慢,循环遍历每个字符在较长的文档中最为明显。在我最初的测试中——一个只有几个单词的文件——它的速度只有 Find 方法的两倍。
另请注意,我最初关闭了ScreenUpdating
每个 Andrew 的子程序,但似乎没有什么区别。