Skip to content

Instantly share code, notes, and snippets.

@mrowles
Created May 10, 2012 03:07
Show Gist options
  • Save mrowles/2650773 to your computer and use it in GitHub Desktop.
Save mrowles/2650773 to your computer and use it in GitHub Desktop.
Microsoft Word: Count occurrence of specific word/s in a document
Function CountWordsOccurrence() As Integer
Application.ScreenUpdating = False
Dim sWords As String
Dim iCount As Integer
'The word which is being counted
sWords = "word"
'Reset counter for each new file
iCount = 0
'Search the document for each occurence of "word":
With Selection
.HomeKey Unit:=wdStory
With .Find
.MatchCase = True
.ClearFormatting
.Text = sWords
Do While .Execute
iCount = iCount + 1
Selection.MoveRight
Loop
End With
'Show the number of occurences
Debug.Print sWords & " appears " & iCount & " times"
End With
Application.ScreenUpdating = True
CountWord = iCount
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment