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?

[Webinar] Streamline your web hosting managementRegister Today

Martin LissConnect With a Mentor Retired ProgrammerCommented:
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.
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 LissRetired ProgrammerCommented:
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.
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Martin LissRetired ProgrammerCommented:
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 LissRetired ProgrammerCommented:
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 LissRetired ProgrammerCommented:
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
All Courses

From novice to tech pro — start learning today.