• Counting characters (XP/2K)

    Author
    Topic
    #377023

    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

    Viewing 3 reply threads
    Author
    Replies
    • #619794

      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

    • #619795

      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

    • #619877

      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 grin).

      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”.

      cheers Klaus

    • #619988

      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

      • #620005

        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”. grin

        • #620007

          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.

          cheers Klaus

        • #620134

          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)

    Viewing 3 reply threads
    Reply To: Counting characters (XP/2K)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: