May be I didn't put it in a proper way. How can I just get 100 questions without the appearance of the input box???

file for macro.png

This will make the process quick. If I need to change it, say to 50, I will then change in the code itself. Any help???

Quote Originally Posted by gmaxey View Post
Kilroy,

Yes. It was interesting. In fact, I've played around with the concept a little and decided to start with Test Bank.docm file containing 500 (or any number of questions) and and a Test Generator template with code to generate a test with a user defined number of questions drawn randomly from the test bank.

Here is the code (for simplicity, the code required the Test Bank.docm and Test Generator.dotm files to be located in same folder).

Maybe mana will come back showing how to do the same thing with the system.random. I can't find any information for working with it to say return 100 random out of a possible 500.


Option Explicit

Sub CreateTest()
Dim oDocBank As Document
Dim oBankTbls As Tables
Dim oRng As Range, oRngInsert As Range
Dim lngCount As Long, lngIndex As Long, lngQuestions As Long
Dim varRanNums
Dim oFld As Field

  Set oDocBank = Documents.Open(ThisDocument.Path & "\Test Bank.docm", , , False, , , , , , , , False)
  Set oBankTbls = oDocBank.Tables
  Application.ScreenUpdating = False
  lngCount = oBankTbls.Count
  lngQuestions = CLng(InputBox("Enter the number of test questions", "NUMBER OF QUESTIONS", "100"))
  varRanNums = fcnRandomNonRepeatingNumberGenerator(1, lngCount, lngQuestions)
  Application.ScreenUpdating = False
  For lngIndex = 1 To UBound(varRanNums)
    Set oRngInsert = ActiveDocument.Bookmarks("QuestionAnchor").Range
    Set oRng = oBankTbls(varRanNums(lngIndex)).Range
    With oRngInsert
      .FormattedText = oRng.FormattedText
      .Collapse wdCollapseEnd
      .InsertBefore vbCr
      .Collapse wdCollapseEnd
    End With
    ActiveDocument.Bookmarks.Add "QuestionAnchor", oRngInsert
    
  Next
  Application.ScreenUpdating = True
  For Each oFld In ActiveDocument.Fields
    If InStr(oFld.Code, "Bank") > 0 Then oFld.Unlink
  Next oFld
  ActiveDocument.Fields.Update
  oDocBank.Close wdDoNotSaveChanges
lbl_Exit:
  Set oDocBank = Nothing: Set oBankTbls = Nothing
  Set oRng = Nothing: Set oRngInsert = Nothing: Set oFld = Nothing
  Exit Sub
End Sub


Function fcnRandomNonRepeatingNumberGenerator(LowerNumber As Long, UpperNumber As Long, _
                                              NumRange As Long) As Variant
Dim lngNumbers As Long, lngRandom As Long, lngTemp As Long
ReDim varNumbers(LowerNumber To UpperNumber) As Variant
Dim varRandomNumbers() As Variant
     
  For lngNumbers = LowerNumber To UpperNumber
    varNumbers(lngNumbers) = lngNumbers
  Next lngNumbers
  Randomize
  For lngNumbers = 1 To NumRange
    lngRandom = Int(Rnd() * (UpperNumber - LowerNumber + 1 - (lngNumbers - 1))) + (LowerNumber + (lngNumbers - 1))
    lngTemp = varNumbers(lngRandom)
    varNumbers(lngRandom) = varNumbers(LowerNumber + lngNumbers - 1)
    varNumbers(LowerNumber + lngNumbers - 1) = lngTemp
  Next lngNumbers
  ReDim Preserve varNumbers(LowerNumber To LowerNumber + NumRange - 1)
  ReDim varRandomNumbers(1 To NumRange)
  For lngNumbers = 1 To NumRange
    varRandomNumbers(lngNumbers) = varNumbers(LBound(varNumbers) + lngNumbers - 1)
  Next lngNumbers
  fcnRandomNonRepeatingNumberGenerator = varRandomNumbers
lbl_Exit:
  Exit Function
End Function

Sub ClearQuestions()
Dim lngIndex As Long
Dim oRng As Range
 For lngIndex = ActiveDocument.Tables.Count To 1 Step -1
   Set oRng = ActiveDocument.Tables(lngIndex).Range.Paragraphs(1).Range
   ActiveDocument.Tables(lngIndex).Delete
   oRng.Delete
Next
End Sub