我有一个带有代码示例的大文档。我想知道字体Calibri(Body)中所有文本的字数,无论大小。我想忽略Consolas等。
我有一个以斜体计算的宏(作为示例发布)但无法让它运行。
Sub IgnoreItalics()
Dim lngWord As Long, lngCountIt As Long
lngCountIt = 0
For lngWord = 1 To ActiveDocument.Words.Count
If ActiveDocument.Words(lngWord).Italic Then
lngCountIt = lngCountIt + 1
End If
Next lngWord
MsgBox "Number of non-italic words: " & _
ActiveDocument.BuiltInDocumentProperties("Number of words") -
lngCountIt
End Sub
知道如何将此更改为Consolas吗?
答案 0 :(得分:2)
修改您的代码以便您可以理解它,这是一个适合我的解决方案
Sub CountTypeface()
Dim lngWord As Long
Dim lngCountIt As Long
Const Typeface As String = "Calibri"
For lngWord = 1 To ActiveDocument.Words.Count
'Ignore any document "Words" that aren't real words (CR, LF etc)
If Len(Trim(ActiveDocument.Words(lngWord))) > 1 Then
If ActiveDocument.Words(lngWord).Font.Name = Typeface Then
lngCountIt = lngCountIt + 1
End If
End If
Next lngWord
MsgBox "Number of " & Typeface & " words: " & lngCountIt
End Sub
答案 1 :(得分:1)
为了记录,更改代码只是 tad 为我工作:
Sub CountFonts()
Dim lngWord As Long, lngCountIt As Long
lngCountIt = 0
For lngWord = 1 To ActiveDocument.Words.Count
If ActiveDocument.Words(lngWord).Font.Name = "Calibri" Then
lngCountIt = lngCountIt + 1
End If
Next lngWord
MsgBox "Number of non-Calibri words: " & _
ActiveDocument.BuiltInDocumentProperties("Number of words") - lngCountIt
End Sub
答案 2 :(得分:-1)
使用
ActiveDocument.ComputeStatistics(wdStatisticWords)
或
ActiveDocument.ComputeStatistics(0)