Vba 是否按字体计算Microsoft Word文档中的字数?

Vba 是否按字体计算Microsoft Word文档中的字数?,vba,fonts,ms-word,Vba,Fonts,Ms Word,我有一个包含代码示例的大型文档。我想知道字体Calibri(Body)中所有文本的字数,无论大小。我想忽略控制台等 我有一个宏按斜体计数(作为示例发布),但无法运行它 Sub IgnoreItalics() Dim lngWord As Long, lngCountIt As Long lngCountIt = 0 For lngWord = 1 To ActiveDocument.Words.Count If ActiveDocument.Words

我有一个包含代码示例的大型文档。我想知道字体Calibri(Body)中所有文本的字数,无论大小。我想忽略控制台等

我有一个宏按斜体计数(作为示例发布),但无法运行它

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

您知道如何将此更改为ConsoleAS吗?

请注意,更改您的代码对我来说有点用:

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

修改您的代码以便您能够理解它,这里有一个适合我的解决方案

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
使用


这有什么不对的?您是否收到错误、无效结果等?我想可能是文档,它有31k字长,所以上次在我的环境下崩溃时,可能是代码。很有趣。在代码中,您同时使用了
ActiveDocument.Words.Count
ActiveDocument.BuiltInDocumentProperties(“字数”)
。你有没有试过在两个位置上都使用just-on或the-other?看起来很有效,我会在代码完成后标记为correct。word在mo上没有响应,但它是一个大文档,所以希望能抓紧时间。
ActiveDocument.ComputeStatistics(wdStatisticWords)
ActiveDocument.ComputeStatistics(0)