Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

Macro causes Word to freeze

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Larry - 09 Jan 2005 22:00 GMT
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
Jonathan West - 09 Jan 2005 22:34 GMT
Try setting the Wrap property to wdFindStop

Signature

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

> 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
[quoted text clipped - 58 lines]
>
> End Sub
Larry - 10 Jan 2005 09:02 GMT
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

> Try setting the Wrap property to wdFindStop
>
[quoted text clipped - 66 lines]
> >
> > End Sub
and - 10 Jan 2005 08:48 GMT
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
-----------------------
Larry - 10 Jan 2005 19:24 GMT
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

> Hi Larry,
>
[quoted text clipped - 39 lines]
> End Function
> -----------------------
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.