Random test generator

L

Laura Guest

Is there a way to set up a macro in Word to randomly
select 50 questions from a list of 200 that are typed in
Word?
 
J

Jezebel

Any number of ways (should we suggest one at random?) Seriously though, you
need to provide a little more information. Where do the questions come from?
If they are just entered in the body of the document, how does the macro
know where one ends and the next starts? What do you need the macro to do
with the selected question?
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

There was a teacher came to these groups a while back who wanted a way to
scramble the order of questions on each copy of a test paper that he
prepared for his students, to catch out those who just copied someone else's
answers. I put the following together for him. The only thing was that it
created a real nightmare for him to mark the test:

Dim i As Integer, j As Integer, k As Integer, Source As Document, Target As
Document, students As Integer
Dim Message, Title, question As Range
Message = "Enter number of students" ' Set prompt.
Title = "Randomizer" ' Set title.
' Display message, title
students = InputBox(Message, Title)


For k = 1 To students
Set Source = Documents.Open("E:\Worddocs\source1.doc")
Set Target = Documents.Add
For i = 100 To 0 Step -1
j = Int((i * Rnd) + 1)
Set question = Source.Paragraphs(j).Range
Target.Range.InsertAfter question
question.Delete
Next i
' Print and Close Document with random list of questions
Target.PrintOut
' Target.SaveAs ("E:\worddocs\student" & k & ".doc")
Target.Close SaveChanges:=wdDoNotSaveChanges
Source.Close SaveChanges:=wdDoNotSaveChanges
Next k

Maybe you can make use of it to create what you want.
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
P

Peter Hewett

Hi Laura

I've had to do things similar to this a number of times. The easy way is to
use AutoText entries (stored in the template) and then use a "card
shuffling algorithm" to randomise them, then select the first however many
you need and insert them into your document.

Using AutoText allows the questions to be ammended or added to without
needing to change any code.

Public Sub InsertRandomAutoText()
Const cNoOfATRequired As Integer = 50

Dim rngLocation As Word.Range
Dim tplAttached As Word.Template
Dim lngRandMax As Long
Dim lngIndex As Long
Dim lngSwapWith As Long
Dim lngTemp As Long
Dim alngNumbers() As Long

' Highest random number that we need to generate
Set tplAttached = ActiveDocument.AttachedTemplate
lngRandMax = tplAttached.AutoTextEntries.Count
If lngRandMax < cNoOfATRequired Then
MsgBox "There are insufficient AutoText entries in the" & _
vbCr & "at least " & cNoOfATRequired & " are required"
Exit Sub
End If

' The starting point is a list of sequential
' numbers, one for each AutoText entry
ReDim alngNumbers(1 To lngRandMax)
For lngIndex = 1 To lngRandMax
alngNumbers(lngIndex) = lngIndex
Next

' Seed random number generator
Randomize

' Now randomise the list we've just generated
For lngIndex = 1 To lngRandMax
lngSwapWith = Int(lngRandMax * Rnd + 1)

' Swap a randomly selected entry with the index entry
lngTemp = alngNumbers(lngIndex)
alngNumbers(lngIndex) = alngNumbers(lngSwapWith)
alngNumbers(lngSwapWith) = lngTemp
Next

' Now insert the first cNoOfATRequired autotext entries
' at the end of the document
Set rngLocation = ActiveDocument.Content
rngLocation.Collapse wdCollapseEnd
For lngIndex = 1 To cNoOfATRequired

' Insert a random AutoText entry
Set rngLocation = _
tplAttached.AutoTextEntries(alngNumbers(lngIndex)).Insert _
(rngLocation, True)
rngLocation.Collapse wdCollapseEnd
Next
End Sub


HTH + Cheers - Peter
 
S

Sonny Maou

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS
wrote:
There was a teacher came to these groups a while back who wanted a way to
scramble the order of questions on each copy of a test paper that he
prepared for his students, to catch out those who just copied someone else's
answers. I put the following together for him. The only thing was that it
created a real nightmare for him to mark the test:

What he needed, then, was the ability to scan the test and have the PC
grade it. I can envision some kind of barcode for each unique question,
although only for multiple choice questions.

OR, print out each question on a separate page, then distribute the
pages in variable order to each student, then reorder them when grading.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top