Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Search Statistics (VBA/Word/2003)

    When searching through a document (Ctrl F), I have the option of finding all instances of the required string and displaying the number of hits.

    I have tried to record this without success.

    Is anyone aware of a VBA technique to extract this data from the Find dialog? I suspect that on a large document it will be significantly faster than going through the document and incrementing a counter on each instance of finding the string.

    TIA
    Attached Images Attached Images
    Regards
    Don

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

    Re: Search Statistics (VBA/Word/2003)


  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Search Statistics (VBA/Word/2003)

    There is no support in VBA for the Find All option. You might use the following:

    Dim strText As String
    Dim strSearch As String
    Dim lngLenBefore As Long
    Dim lngLenAfter As Long
    Dim lngLenSearch As Long
    Dim lngCount As Long

    ' Replace with the text whose occurrences you want to count.
    strSearch = "quick"
    lngLenSearch = Len(strSearch)
    strText = ActiveDocument.Content.Text
    lngLenBefore = Len(strText)
    strText = Replace(strText, strSearch, "")
    lngLenAfter = Len(strText)
    lngCount = (lngLenBefore - lngLenAfter) / lngLenSearch
    MsgBox lngCount

  4. #4
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Search Statistics (VBA/Word/2003)

    Thank you Stuart
    I will be using the CountWord function with one small revision. The second last line should read:
    Application.ScreenUpdating = True

    Not:
    Application.ScreenUpdating = False
    Regards
    Don

  5. #5
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Search Statistics (VBA/Word/2003)

    Hans
    Thank you. Elegant and blindingly fast. <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>
    Regards
    Don

Posting Permissions

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