Results 1 to 8 of 8
  1. #1
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Jerusalem, Israel
    Posts
    708
    Thanks
    0
    Thanked 1 Time in 1 Post

    Counting characters (XP/2K)

    I might be reinventing the wheel , but I would like to count the number of timers a particular character appears in a doc. I wrote the following code:
    Public Sub howMany()
    Dim intHow As Integer
    Dim strWhat As String
    Dim intCount As Integer
    Dim myresult As Integer

    strWhat = InputBox("Which character would you like to search for?", "Search character ")
    intHow = 0
    intCount = 0

    With ActiveDocument.Content.find
    .ClearFormatting

    Do While .Execute(FindText:=strWhat, Forward:=True, _
    Format:=True) = True
    intHow = intHow + 1

    End If
    Loop
    End With
    x = MsgBox("The character " & strWhat & " appears " & intHow & " times", vbInformation, "Results")
    End Sub

    It works, but if the character appears more than 1000 times my computer hangs.
    Is there something that does this, or a better way to do it. I thought maybe if I break the doc up and count a paragraph ata time, then it will not hang.
    Thanks

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Counting characters (XP/2K)

    Zave,

    The integer data type cannot hold a number larger than 32,767 - don't know if that is relevant since you are getting the problem when you reach 1,000, but in any case you might try replacing all of the "Integer"s with "Long"s.

    Gary

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Counting characters (XP/2K)

    For a different approach you could look at the article subtitled How to find out how many occurrences there are of a particular word in a document on the Word MVP FAQ Site

    StuartR

  4. #4
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting characters (XP/2K)

    Hi Zave,

    You have a redundant "End If" in the "Do While .Execte ...Loop" which might be responsible for the hangs.

    Two alternate approaches that may be quite a bit faster:

    Get the count of characters, replace the character with nothing, then count the charactes again. Use Application.Undo to undo the changes (... this is not for the faint-hearted <img src=/S/grin.gif border=0 alt=grin width=15 height=15>).

    Or get the whole main story in a string (myString = ActiveDocument.Content.Text), and use the Replace function to replace the character with nothing (again taking the difference in string length before and after).

    It takes about the same time to get the document text into a string variable as it takes to search the document once using "Find/Replace (Replace All)".
    But if you need the counts of different characters, the Replace on the string will be much faster than "Find/Replace".

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  5. #5
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Jerusalem, Israel
    Posts
    708
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Counting characters (XP/2K)

    this works if anyone is interested. It is based on the helpful input of everyone who responded:

    Public Sub howMany()

    Dim strfind As String
    Dim lngMany As Long
    Dim lngBefore As Long
    Dim lngAfter As Long
    Dim lnghow As Long

    strfind = InputBox("Which Character would you like to search for?", "Count")


    ' this protects the original doc
    Selection.WholeStory
    Selection.copy
    Documents.Add DocumentType:=wdNewBlankDocument
    Selection.PasteAndFormat (wdPasteDefault)


    lngBefore = ActiveDocument.Characters.Count
    StatusBar = lngBefore

    Selection.find.ClearFormatting
    Selection.find.Replacement.ClearFormatting
    With Selection.find
    .Text = strfind
    .Replacement.Text = strfind & "#"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.find.Execute Replace:=wdReplaceAll

    lngAfter = ActiveDocument.Characters.Count
    StatusBar = lngAfter
    lnghow = lngAfter - lngBefore
    StatusBar = lnghow
    x = MsgBox("The character " & strfind & " appears " & lnghow & " times", vbInformation, "Results")
    ActiveDocument.Close (False) 'to close the temporary doc that you opened.

    End Sub

  6. #6
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting characters (XP/2K)

    How about just a Find/Replace. Find the character, replace with itself. You then get a message box that Word completed the search & made xxx replacements. I did this on a several hundred page document on the letter "e" & got 87,625. Just to be on the safe side, I'd make a copy of the document first. (Hate to have a documents without any "eeees". <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  7. #7
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting characters (XP/2K)

    It's a pretty bad omission that you can't get the number of replacements with VBA. The number of replacements seems to be displayed with an error handler in the user interface, but the error (5524) can't be caught in a macro.

    I'd hoped there would be a way in XP/2002, since many have complained in the last versions.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  8. #8
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Jerusalem, Israel
    Posts
    708
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Counting characters (XP/2K)

    The reason I did it with VBA is for a complete novice user, he can get the count easily, without having to open unfamiliar menus and potentially destroying his doc (no eees)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •