在分析文档时,您可能想知道是否有一种方法可以对文档中的单词数进行计数。不幸的是,Word没有包含这样的功能,但是您可以做几件事。

首先,如果您想知道使用特定单词或短语的次数,可以按照以下步骤操作:

。按Ctrl + H以显示“查找和替换”对话框的“替换”选项卡。 (请参见图1。)

。在“查找内容”框中,输入要计数的单词或短语。

。在“替换为”框中,输入^&。此字符序列告诉Word您要用在“查找内容”框中放置的内容替换找到的内容。 (换句话说,您正在用单词或短语替换它本身。)

。如果要搜索单个单词,请确保单击“仅查找整个单词”复选框。

。单击全部替换。 Word进行替换,并向您显示替换了多少个实例。那就是你想要的电话号码。

如果您只想知道一个或两个单词或短语,则此方法非常有用。您可以使用宏在文档中搜索并为您计数,从而使该过程稍微自动化。下面的宏提示用户输入单词,然后计算该单词在文档中出现的次数。它将继续询问另一个单词,直到用户单击“取消”按钮。

Sub FindWords()

Dim sResponse As String     Dim iCount As Integer

' Input different words until the user clicks cancel     Do         ' Identify the word to count         sResponse = InputBox( _           Prompt:="What word do you want to count?", _           Title:="Count Words", Default:="")

If sResponse > "" Then             ' Set the counter to zero for each loop             iCount = 0             Application.ScreenUpdating = False             With Selection                 .HomeKey Unit:=wdStory                 With .Find                     .ClearFormatting                     .Text = sResponse                     ' Loop until Word can no longer                     ' find the search string and                     ' count each instance                     Do While .Execute                         iCount = iCount + 1                         Selection.MoveRight                     Loop                 End With                 ' show the number of occurences                 MsgBox sResponse & " appears " & iCount & " times"

End With             Application.ScreenUpdating = True         End If     Loop While sResponse <> ""

End Sub

如果要确定文档中所有唯一的单词以及每个单词在文档中出现的次数,则需要一种不同的方法。下面的宏就可以做到这一点。

Sub WordFrequency()

Const maxwords = 9000          'Maximum unique words allowed     Dim SingleWord As String       'Raw word pulled from doc     Dim Words(maxwords) As String  'Array to hold unique words     Dim Freq(maxwords) As Integer  'Frequency counter for unique words     Dim WordNum As Integer         'Number of unique words     Dim ByFreq As Boolean          'Flag for sorting order     Dim ttlwds As Long             'Total words in the document     Dim Excludes As String         'Words to be excluded     Dim Found As Boolean           'Temporary flag     Dim j, k, l, Temp As Integer   'Temporary variables     Dim ans As String              'How user wants to sort results     Dim tword As String            '     Dim aword As Object            '     Dim tmpName As String          '

' Set up excluded words     Excludes = "[the][a][of][is][to][for][by][be][and][are]"



' Find out how to sort     ByFreq = True     ans = InputBox("Sort by WORD or by FREQ?", "Sort order", "WORD")

If ans = "" Then End     If UCase(ans) = "WORD" Then         ByFreq = False     End If          Selection.HomeKey Unit:=wdStory     System.Cursor = wdCursorWait     WordNum = 0     ttlwds = ActiveDocument.Words.Count

' Control the repeat     For Each aword In ActiveDocument.Words         SingleWord = Trim(LCase(aword))

'Out of range?

If SingleWord < "a" Or SingleWord > "z" Then             SingleWord = ""

End If         'On exclude list?

If InStr(Excludes, "[" & SingleWord & "]") Then             SingleWord = ""

End If         If Len(SingleWord) > 0 Then             Found = False             For j = 1 To WordNum                 If Words(j) = SingleWord Then                     Freq(j) = Freq(j) + 1                     Found = True                     Exit For                 End If             Next j             If Not Found Then                 WordNum = WordNum + 1                 Words(WordNum) = SingleWord                 Freq(WordNum) = 1             End If             If WordNum > maxwords - 1 Then                 j = MsgBox("Too many words.", vbOKOnly)

Exit For             End If         End If         ttlwds = ttlwds - 1         StatusBar = "Remaining: " & ttlwds & ", Unique: " & WordNum     Next aword

' Now sort it into word order     For j = 1 To WordNum - 1         k = j         For l = j + 1 To WordNum             If (Not ByFreq And Words(l) < Words(k)) _               Or (ByFreq And Freq(l) > Freq(k)) Then k = l         Next l         If k <> j Then             tword = Words(j)

Words(j) = Words(k)

Words(k) = tword             Temp = Freq(j)

Freq(j) = Freq(k)

Freq(k) = Temp         End If         StatusBar = "Sorting: " & WordNum - j     Next j

' Now write out the results     tmpName = ActiveDocument.AttachedTemplate.FullName     Documents.Add Template:=tmpName, NewTemplate:=False     Selection.ParagraphFormat.TabStops.ClearAll     With Selection         For j = 1 To WordNum             .TypeText Text:=Trim(Str(Freq(j))) _               & vbTab & Words(j) & vbCrLf         Next j     End With     System.Cursor = wdCursorNormal     j = MsgBox("There were " & Trim(Str(WordNum)) & _       " different words ", vbOKOnly, "Finished")

End Sub

当您打开文档并运行此宏时,系统会询问您是否要创建按单词或频率排序的列表。如果选择单词,则结果列表将按字母顺序显示。如果选择频率,则根据单词在文档中出现的次数,结果列表将按降序排列。

在宏运行时,状态栏指示正在发生的事情。

根据文档的大小和计算机的速度,宏可能需要一段时间才能完成。 (我用一份719页的文档运行了超过34.9万个单词,大约花了五分钟的时间。)

请注意,宏中有一行在Excludes字符串中设置值。该字符串包含将单词列表放在一起时宏将忽略的单词。如果要将单词添加到排除列表,只需将它们添加到[方括号]之间的字符串中。另外,请确保排除词为小写。

如果您由于某些原因不喜欢使用宏,则可以使用其他程序来创建字数统计。例如,NoteTab文本编辑器(“轻型”版本可从https://www.notetab.com/免费下载)具有提供字数统计功能。

您需要做的就是复制整个文档并将其粘贴到NoteTab中。然后,在NoteTab中,选择“工具” |“工具”。文字统计|更多。它提供了对单词频率(包括百分比)的分析。

注意:

如果您想知道如何使用此页面(或_WordTips_网站上的任何其他页面)中描述的宏,我准备了一个包含有用信息的特殊页面。

_WordTips_是您进行经济有效的Microsoft Word培训的来源。

(Microsoft Word是世界上最流行的文字处理软件。)此技巧(10761)适用于Microsoft Word 2007、2010、2013和2016。您可以在这里找到适用于Word较旧菜单界面的该技巧的版本: