Results 1 to 8 of 8
  1. #1
    Silver Lounger
    Join Date
    Jan 2001
    Location
    West Long Branch, New Jersey, USA
    Posts
    1,921
    Thanks
    6
    Thanked 9 Times in 7 Posts

    Can it count? (2003)

    Hi All,

    I've run across a problem in Word 2003 that I didn't find addressed in the Word board or the MSKB (although it could be the words I used).

    Suppose I want to find the number of occurrences of a word in a selection. The selection may be contiguous, empty (just the cursor at some position but nothing selected so implying the whole doc), or non-contiguous. Certainly in 2003, one can do non-contiguous selections. I think this was also in Word XP/2002, although I don't have that on any machines to test. If non-contiguous selections were in XP, I'd assume the results would be the same.

    Use replace and set the Find What="desired word" and Replace With="desired word" or find-what-text - doesn't matter.

    No problems with the 1st 2 cases (contiguous and empty) in terms of the count of replacements reported (the msg that says xx replacements were made).

    But the 3rd case (non-contiguous) gives strange results.

    I put together a simple document (see attached) of 9 paras of 5 sentences of "The quick brown fox..." using =rand. I chose "brown" as my subject word (maybe not a great choice-you'll see why). In the first 2 cases, 45 replacements of brown were reportedly made. Makes sense (9 paras x 5 sentences/para x 1 "brown"/sentence = let me see, don't tell me...[smilie for counting on fingers] 45!). In the 3rd case with 2 non-contiguous paras selected, 15 replacements were reportedly made!!!

    Sinnse I ain't dat gude inn mass, I assoommed dat I kuldn't kount or it was a problem not having enough fingers and toes. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> I asked Word to show me the 15 replacements by adding to the RW spec to change the color of the replacements to red. It still gave a report of 15 altho there were only 10 red brown's. When I let Word search the rest of the document, Word showed where it really shines: it reported 50 replacements made in the rest of the doc and 85 in total.

    OK. Maybe this is the user interface. So I decided to try VBA. I recorded a macro (maybe a mistake). First 2 cases are fine. Then 3rd case: same 2 non-contiguous paras selected. Run the macro. Same report of 15 replacements but only 10 red brown's shown. I also let Word check the rest of the document. Here's where Word continued to show where it shined/shoned/shone: I got reports (on different runs of the macro) of 20, 55, and 75 for the brown's in the rest of the document.

    Anyway, attached is my sample doc - play around and let me know if you get something that makes a little more sense.

    Thanks.

    Fred
    Attached Files Attached Files

  2. #2
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Tallahassee, Florida, USA
    Posts
    901
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can it count? (2003)

    Hi Fred! That's a bizarre one all right. Just for grins and giggles I tried it with XP (and you're correct that non-contiguous selections was introduced in WordXP). It's fine with the entire document, and with contiguous selections. When I do the non-contiguous selections my results differ in that it reads the first action correctly (e.g. it tells me that it made 10 replacements) but when I tell it to continue with the rest of the document it tells me that there are 50 replacements.
    Karen

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

    Re: Can it count? (2003)

    It's even worse than that. I tried using the trick on the MVPS site to count words replaced in VBA. If you have a non-contiguous selection and you do a Selection.Find with a replace then it behaves most bizarrely - replacing multiple times in the selected regions and once in the non selected ones.

    StuartR

  4. #4
    Silver Lounger
    Join Date
    Jan 2001
    Location
    West Long Branch, New Jersey, USA
    Posts
    1,921
    Thanks
    6
    Thanked 9 Times in 7 Posts

    Re: Can it count? (2003)

    Stuart,

    Maybe that explains the weird numbers I was getting for the total and rest of the replacements. But one would think it was repeatable - ie, get the same wrong numbers each time. But it seems like you may have found some rationality in the madness. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Fred

  5. #5
    Silver Lounger
    Join Date
    Jan 2001
    Location
    West Long Branch, New Jersey, USA
    Posts
    1,921
    Thanks
    6
    Thanked 9 Times in 7 Posts

    Re: Can it count? (2003)

    Hi Stuart,

    Thanks for verifying that I'm not crazy.

    Let me ask if the following approach would be a workaround possible in VBA (for Word XP and beyond):
    - start with the selection as provided by the user
    - assume (the big assumption) there is a way to determine if the selection was a contiguous one or not. If so and if contiguous (or whole doc bcs no selection), use VBA to get number of replacements (something like on the MVPS site). If not contiguous, loop thru a collection of selections (assume there is one). Mark somehow what's selected (maybe insert a bookmark around each contiguous piece).
    - deselect the entire selection
    - loop thru the marked info and select it.
    - do a replace to get the count for that contiguous selection
    - add up the counts for all selections

    If one wants the count in the rest of the document, as the normal find/replace asks, loop thru the marked info and do a replace on those items not marked one contiguous piece at a time and add the replacements.

    Anyway, just an idea.

    Fred

  6. #6
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can it count? (2003)

    Hi Fred,

    I understand it's just the count you want. If so, using VBA you could copy the selection to the clipboard, then add a doc and paste the content of the clipboard into the new doc. Using find/replace on the new doc should give you the correct count, regardless whether the original selection was contiguous or not. You could then close the new doc without saving.

  7. #7
    Silver Lounger
    Join Date
    Jan 2001
    Location
    West Long Branch, New Jersey, USA
    Posts
    1,921
    Thanks
    6
    Thanked 9 Times in 7 Posts

    Re: Can it count? (2003)

    Jan,

    excellent idea. I was thinking along the lines of creating a new document in my last post but something about my thought didn't make sense to me, so I deleted it. Your idea is simpler and has the benefit of actually working (I tested it).

    Fred

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

    Re: Can it count? (2003)

    This is a nice solution.

    I tried to avoid poluting the clipboard with
    documents.Add.Content=selection.Text
    but it only gets the last range in the selection.

    The implementation of discontinuous selections is so badly done and incomplete that it makes lots of problems.

    StuartR

Posting Permissions

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