Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9268979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:59:37+00:00 2026-06-18T14:59:37+00:00

My wife is a professor, and I found out she’s been manually creating randomized

  • 0

My wife is a professor, and I found out she’s been manually creating randomized versions of her tests (to reduce cheating) by hand for years, along with all of the other faculty in her department. She uses Word 2007 and 2010 to write her tests, so I set about writing a VBA macro to do this tedious process for her.

Her tests include images, lists, and other formatting, so straight text copying won’t work. All questions that reference the same images are on the same page, otherwise each question gets it’s own page. The first page contains instructions and needs to be included at the beginning of a randomized test doc, but all other pages need to be randomized in a new document. After the randomization process, I am removing page breaks so that questions are neatly on as few pages as possible.

So far I haven’t been able to transfer Ranges taken from the Page collection to the new document without losing the formatting information. I’ve googled all over the place, but I haven’t found any indications of what I’m doing wrong yet.

My code thus far:

Sub CreateTestVersions()

Dim ThisDoc As Document
Dim NewDoc As Document
Dim Pgs As pages
Dim Question As Range

Let Skip = 1 'Number of pages to skip randomizing

Set ThisDoc = Application.ActiveDocument
Set NewDoc = Documents.Add 'Create new document
Set Pgs = ThisDoc.Windows(1).Panes(1).pages 'Pages collection

ReDim Questions(1 To Pgs.Count - Skip) As Range

For p = 1 To Skip 'Add skipped pages to begining of new document
    NewDoc.Content = NewDoc.Content & Pgs(p).Rectangles(1).Range
Next

' Add questions to an array of ranges
For q = LBound(Questions) To UBound(Questions)
    Set Question = Pgs(q + Skip).Rectangles(1).Range

    'Keep questions on a single page, don't split accross pages
    Question.Paragraphs.KeepTogether = True

    ' All lists, text formatting, etc. is lost for some reason
    Set Questions(q) = Question ' Needs fixed
Next

'Randomization needs to happen here

'Add randomized questions to new document
For q = LBound(Questions) To UBound(Questions)
    NewDoc.Content = NewDoc.Content & Questions(q)
Next

'Remove page breaks
With NewDoc.Content.Find
    .Text = "^m"
    .Forward = True
    .Wrap = wdFindStop
    .Replacement.Text = ""
    .Execute Replace:=wdReplaceAll
End With
End Sub

I’m using the Questions array because I figure that will be easier to randomize, especially when I expand this code to generate more than one version. I’d also like to avoid using Select, Copy, Paste if at all possible.

Any insight on why I’m losing formatting and what the proper approach should be is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:59:39+00:00Added an answer on June 18, 2026 at 2:59 pm

    I did manage to get this to work using InsertFile and adding ranged bookmarks around each question. Here is the finished product. Hopefully it will help some other people out!

    Sub CreateTestVersions()
    
    Dim ThisDoc As Document
    Dim NewDocs() As Document
    Dim Pgs As pages
    Dim Question As Range
    Dim skip As Variant
    Dim versions As Variant
    Dim Vers() As Integer
    Dim qList As String
    Dim numQs As Integer
    Dim bound() As String
    Dim fileName() As String
    Dim pages As Integer
    Dim minPages As Integer
    Dim tryAgain As Boolean
    Dim all As Range
    
    Set ThisDoc = Application.ActiveDocument
    Set Pgs = ThisDoc.ActiveWindow.Panes(1).pages 'Pages collection
    
    'Number of pages to skip randomizing
    skip = InputBox( _
        "Each question should be on its own page, " _
        & "unless that question shares a connection with another " _
        & "(e.g. they share an image reference).  You can separate " _
        & "them using CTRL-Enter or Insert Page Break." & vbNewLine & vbNewLine _
        & "How many pages belong at the beginning of every version " _
        & "(instructions, personal data, etc.)?", "Question", 1)
    
    If skip = "" Then Exit Sub
    
    versions = InputBox("How many versions would you like to produce?", "Question", 4)
    
    If versions = "" Then Exit Sub
    
    numQs = Pgs.Count - skip
    
    qList = InputBox(numQs & " question pages detected. Please list which questions" _
        & " you want to use, with ranges denoted with dashes and gaps by commas" _
        & " (e.g. 1-5, 9, 12-20).", "Question", "1-" & numQs)
    
    If qList = "" Then Exit Sub
    
    ReDim NewDocs(1 To versions) As Document
    ReDim Vers(1 To versions) As Integer
    For v = 1 To versions
        'Create new document(s)
        Set NewDocs(v) = Documents.Add
        Vers(v) = v
    Next
    
    ReDim Indexes(1 To numQs) As Long
    qList = Replace(qList, " ", "")
    RangeList = Split(qList, ",")
    numQs = 0
    For Each rng In RangeList
        bound = Split(rng, "-")
        For i = bound(LBound(bound)) To bound(UBound(bound))
            numQs = numQs + 1
            Indexes(numQs) = i
        Next
    Next
    
    ReDim Preserve Indexes(1 To numQs) As Long
    ReDim Questions(1 To numQs) As Range
    
    ' Add questions to an array of ranges
    For Each q In Indexes
        If (Not ThisDoc.Bookmarks.Exists("Question " & q)) Then
            ThisDoc.Bookmarks.Add "Question" & q, _
                              Pgs(q + skip).Rectangles(1).Range
        End If
    Next
    
    minPages = Pgs.Count
    Randomize
    Do
        For Each v In Vers
            'Clear new document in case we are retrying for a shorter version
            Set all = NewDocs(v).Content
            all.WholeStory
            all.Select
            Selection.Delete
            'Add skipped pages to begining of new document
            If (Not ThisDoc.Bookmarks.Exists("Introduction")) Then
                ThisDoc.Bookmarks.Add "Introduction", _
                    ThisDoc.Range(Pgs(1).Rectangles(1).Range.Start, _
                                  Pgs(skip).Rectangles(1).Range.End)
            End If
            NewDocs(v).Content.InsertFile ThisDoc.FullName, "Introduction"
    
            'Generate random indexs
            For i = numQs To 2 Step -1
                r = Int(Rnd() * (i - 2)) + 1
                temp = Indexes(r)
                Indexes(r) = Indexes(i)
                Indexes(i) = temp
            Next i
    
            'Add randomized questions to new document
            For q = LBound(Questions) To UBound(Questions)
                i = Indexes(q)
                Set Question = NewDocs(v).Content
                Question.Collapse Direction:=wdCollapseEnd
                Question.InsertFile ThisDoc.FullName, "Question" & i
                Set Question = NewDocs(v).Range(Question.Start, NewDocs(v).Range.End)
                Question.Paragraphs.KeepWithNext = True
                NewDocs(v).Bookmarks.Add "Question" & i, Question
            Next
    
            'Remove page breaks
            With NewDocs(v).Content.Find
                .Text = "^m"
                .Forward = True
                .Wrap = wdFindContinue
                .Replacement.Text = ""
                .Execute Replace:=wdReplaceAll
            End With
    
            'Group questions within pages, not accross them
            For Each Bookmark In NewDocs(v).Bookmarks
                Bookmark.Range.Paragraphs.Last.KeepWithNext = False
            Next
    
            pages = NewDocs(v).Windows(1).Panes(1).pages.Count
            If pages < minPages Then minPages = pages
        Next
    
        ' If all pages are not minimum length then try again
        tryAgain = False
        For Each v In Vers
            pages = NewDocs(v).Windows(1).Panes(1).pages.Count
            If pages > minPages Then tryAgain = True
        Next
    Loop While tryAgain
    
    For Each v In Vers
        'Save Document
        fileName = Split(ThisDoc.Name, ".")
        file = fileName(0)
        ext = fileName(1)
        NewDocs(v).SaveAs2 _
                fileName:=file & " Version " & v & "." & ext, _
                CompatibilityMode:=wdCurrent
    Next
    ThisDoc.Activate
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been tasked (by my wife) with creating a program to allow her to
Quick question...my wife is drafting a small agreement for her the IT company she
My wife plays neopets and is constantly editing her profile page's html. I would
I create a website for my wife. She's a teacher and she would like
I am trying to build an app on my wife's phone that pranks her
My wife is working her way through Codecademy (Javascript) and was frustrated by semicolons.
I was talking with my non-techie wife tonight. She was talking about how she
So I build a tiny bingo program for my wife to use at her
My wife has a Bernina embroidery machine and I'd like to experiment with creating
My wife's old work computer has a ClickOnce-deployed app on it that she now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.