2012-03-15 71 views
0

我有一个包含代码示例的大文档。我想知道字体Calibri(Body)中所有文本的字数,而不管大小。我想忽略Consolas等通过字体统计Microsoft Word文档中的单词?

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

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

这是什么不运行?你是否收到错误,结果无效等? – Gaffi 2012-03-15 13:36:15

+0

我认为它可能是文档,它是31k字长,所以它最后一次在我的环境下崩溃,认为它可能是代码。 – 2012-03-15 13:47:17

+0

有趣。在你的代码中,你同时使用'ActiveDocument.Words.Count'和'ActiveDocument.BuiltInDocumentProperties(“字数”)''。您是否尝试过在两个位置使用刚刚或另一个? – Gaffi 2012-03-15 13:53:54

回答

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 
有效的解决方案
+0

这对我最合适 – 2012-03-15 14:26:45

1

为了记录在案,改变你的代码只是一个点点工作对我来说:

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 
+0

看起来像是有效的,一旦代码完成,我会标记正确。这个词在莫文章中没有回应,但它是一个很大的文件,所以希望能够吸引它的时间。 – 2012-03-15 13:52:23

-1

使用

ActiveDocument.ComputeStatistics(wdStatisticWords) 

ActiveDocument.ComputeStatistics(0) 
相关问题