2012-03-15 69 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) 
相關問題