L
Larry
This is a macro I use frequently to count the number of occurences of a
word in a document. It's very fast and convenient. I simply place the
cursor in the word, and run the macro, and it gives me a message box
with the result
But it frequently causes Word to freeze up. Can anyone point to what
may be wrong with it?
Thanks,
Larry
Sub TextCountQuick()
' Counts current word or selected word or string.
Dim myrange As Range
Dim myPhrase As String
Dim i As Long
' Clear Find parameters
With Selection.Find
.Replacement.Text = "": .Format = False: .MatchCase = False
.MatchWholeWord = False: .MatchWildcards = False: .MatchSoundsLike =
False
.MatchAllWordForms = False: .ClearFormatting
End With
Application.ScreenUpdating = False
System.Cursor = wdCursorWait
Set myrange = ActiveDocument.Range
' If there is no selection, select current word.
If Selection.Type <> wdSelectionNormal Then Selection.Words(1).Select
' Unselect any empty space after text.
Selection.MoveEndWhile cset:=" ", Count:=wdBackward
myPhrase = Selection.Text
Selection.Collapse wdCollapseStart
myrange.Find.ClearFormatting
myrange.Find.Replacement.ClearFormatting
With myrange.Find
.Text = myPhrase
.Forward = True
.MatchWholeWord = False
.MatchWildcards = False
' Strange. If I have wdFindContinue, the macro goes into an endless
loop.
' So I've commented this out. This must have to do with fact that I'm
using
' document range rather than selection.
' .Wrap = wdFindContinue
Do While .Execute
i = i + 1
Loop
End With
MsgBox "Occurrences of '" & myPhrase & "' " & i, , "Text Count"
' clear Find parameter
myrange.Find.Text = ""
End Sub
word in a document. It's very fast and convenient. I simply place the
cursor in the word, and run the macro, and it gives me a message box
with the result
But it frequently causes Word to freeze up. Can anyone point to what
may be wrong with it?
Thanks,
Larry
Sub TextCountQuick()
' Counts current word or selected word or string.
Dim myrange As Range
Dim myPhrase As String
Dim i As Long
' Clear Find parameters
With Selection.Find
.Replacement.Text = "": .Format = False: .MatchCase = False
.MatchWholeWord = False: .MatchWildcards = False: .MatchSoundsLike =
False
.MatchAllWordForms = False: .ClearFormatting
End With
Application.ScreenUpdating = False
System.Cursor = wdCursorWait
Set myrange = ActiveDocument.Range
' If there is no selection, select current word.
If Selection.Type <> wdSelectionNormal Then Selection.Words(1).Select
' Unselect any empty space after text.
Selection.MoveEndWhile cset:=" ", Count:=wdBackward
myPhrase = Selection.Text
Selection.Collapse wdCollapseStart
myrange.Find.ClearFormatting
myrange.Find.Replacement.ClearFormatting
With myrange.Find
.Text = myPhrase
.Forward = True
.MatchWholeWord = False
.MatchWildcards = False
' Strange. If I have wdFindContinue, the macro goes into an endless
loop.
' So I've commented this out. This must have to do with fact that I'm
using
' document range rather than selection.
' .Wrap = wdFindContinue
Do While .Execute
i = i + 1
Loop
End With
MsgBox "Occurrences of '" & myPhrase & "' " & i, , "Text Count"
' clear Find parameter
myrange.Find.Text = ""
End Sub