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

Re: Macro causes Word to freeze by Jonathan

Jonathan
Sun Jan 09 16:34:17 CST 2005

Try setting the Wrap property to wdFindStop


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup


"Larry" <larry328@att.net> wrote in message
news:erIL1Zp9EHA.2192@TK2MSFTNGP14.phx.gbl...
>
> 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
>
>


Re: Macro causes Word to freeze by and

and
Mon Jan 10 02:48:08 CST 2005

Hi Larry,

I do this with a small function that is activated with a few lines of
code (they can be joined into a procedure, but I use this function with
many different procedures).

Best regards,
ANDy

=================================

Sub CountStrings()
Dim strFnd As String
strFnd = InputBox("Enter the string to count")
MsgBox ("The string '" & strFnd & "' occurs " & NumOf(strFnd) & " times
in this document.")
End Sub


Public Function NumOf(strT) As Long
'counts # of ocurrences of a string in active document
Selection.HomeKey unit:=wdStory
Selection.MoveLeft unit:=wdCharacter, Count:=1

NumOf = 0

With Selection.Find
.ClearFormatting
.Text = strT
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
If Selection.Find.Found Then NumOf = NumOf + 1
Loop
End With

End Function
-----------------------

Re: Macro causes Word to freeze by Larry

Larry
Mon Jan 10 03:02:07 CST 2005



Hi Jonathan,

I've made the change. I've just understood something I should have
understood years ago. You always use wdFindStop when using the Range
property for a search, and you control for for the wrap by using either
Selection.Range (which is the equivalent of wdFindStop with
Selection.Find) or ActiveDocument.Range (which is the equivalent of
WdFindContinue with Selection.Find).

There. I've exposed my newbiehood for all the world to see.

Larry





"Jonathan West" <jwest@mvps.org> wrote in message
news:e0eISup9EHA.1408@TK2MSFTNGP10.phx.gbl...
> Try setting the Wrap property to wdFindStop
>
>
> --
> Regards
> Jonathan West - Word MVP
> www.intelligentdocuments.co.uk
> Please reply to the newsgroup
>
>
> "Larry" <larry328@att.net> wrote in message
> news:erIL1Zp9EHA.2192@TK2MSFTNGP14.phx.gbl...
> >
> > 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
> >
> >
>



Re: Macro causes Word to freeze by Larry

Larry
Mon Jan 10 13:24:53 CST 2005

Yes, I also have a macro like that, where I enter in an input box the
string to be counted. But this macro is different, I don't have to type
the word. I just have to have the cursor located in a word in the text,
I run the macro and the macro gives me the number of occurrences of that
word in the document. That's why I call it TextCountQuick. I use it
constantly to see if I've been using any word too often in an article.

Larry


"and" <and@nospam.invalid> wrote in message
news:tKydndGRFdfV3H_cRVnyvg@zeelandnet.nl...
> Hi Larry,
>
> I do this with a small function that is activated with a few lines of
> code (they can be joined into a procedure, but I use this function
with
> many different procedures).
>
> Best regards,
> ANDy
>
> =================================
>
> Sub CountStrings()
> Dim strFnd As String
> strFnd = InputBox("Enter the string to count")
> MsgBox ("The string '" & strFnd & "' occurs " & NumOf(strFnd) & "
times
> in this document.")
> End Sub
>
>
> Public Function NumOf(strT) As Long
> 'counts # of ocurrences of a string in active document
> Selection.HomeKey unit:=wdStory
> Selection.MoveLeft unit:=wdCharacter, Count:=1
>
> NumOf = 0
>
> With Selection.Find
> .ClearFormatting
> .Text = strT
> .Forward = True
> .Wrap = wdFindContinue
> .Format = False
> .MatchCase = True
> .MatchWholeWord = False
> .MatchWildcards = False
> .MatchSoundsLike = False
> .MatchAllWordForms = False
> Do While .Execute
> If Selection.Find.Found Then NumOf = NumOf + 1
> Loop
> End With
>
> End Function
> -----------------------