Random sample of words from a Word document

Hi experts!

I am writing a Word VBA macro to show various Grade Level indexes and one of them requires a random sample of 150 words from a Word doc. What would be the best way to randomly choose those 150 words?

Ed MatsuokaPartner/Senior IT SpecialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Here is code that will do that.
Sub RandomWords()
Dim colWords As New Collection
Dim intWord As Integer
Dim x As Integer
Const MAX = 150

With ThisDocument
    Do Until colWords.count = MAX
        intWord = Int((MAX - 1 + 1) * Rnd + 1)
        On Error Resume Next
        colWords.Add .Words(intWord), .Words(intWord)
        On Error GoTo 0
End With

For intWord = 1 To MAX
    Debug.Print colWords(intWord)

End Sub

Open in new window

You should also place the command Randomize somewhere in your code where it only will be done once.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ed MatsuokaPartner/Senior IT SpecialistAuthor Commented:
Hi Martin!

I tried this and it didn't work. Is some code missing, you DIM x but x isn't used anywhere in the macro.
Martin LissOlder than dirtCommented:
The x variable isn't needed.

When you say "it didn't work", what do you mean? Did you get an error message?
Did you look in the Immediate Window via Ctrl+g?

Open the attached doc, go to 'ThisDiocument' and run the macro that you'll find there. (You should insert a module and move the code there. And until you add the Randomize line the results will probably always be  the same.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Martin LissOlder than dirtCommented:
Oh, in that sample I just posted you'll need to change the Const to something less than 150 since there aren't 150 words in the doc.
Ed MatsuokaPartner/Senior IT SpecialistAuthor Commented:
Interesting. When I run it from THIS DOCUMENT it works but when I run the exact same code from a MODULE in my NORMAL.DOT it just churns. Is the reference to THIS DOCUMENT the problem?
Martin LissOlder than dirtCommented:
Yes, change ThisDocument to ActiveDocument.Content
Ed MatsuokaPartner/Senior IT SpecialistAuthor Commented:
Thanks! I tweaked it as you said and it works. Now I am just going to play around with it since it is slow. Have a great day.
Martin LissOlder than dirtCommented:
In that case look for samples of code that use a Dictionary object rather than the Collection object.

In any case you’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.