August 2, 2005

Creating interactive forms with OOo Writer

Author: Dmitri Popov

With OpenOffice.org Writer you can create any kind of form from simple questionnaires to sophisticated interactive tests. Here's how to create a simple quiz using the form tools, and how to add interactivity to it with a couple of macros.The quiz you are going to create will consist of a series of multiple choice questions. Before you start designing the quiz, it is a good idea to create a database that will contain all the questions. This database will greatly simplify the process of managing and adding questions to the quiz. Create a new database using Base, and make sure it is registered for use with other OOo applications by selecting the "Yes, register the database for me" option in the Database Wizard. Create a new table and populate it with fields. Which fields you want to use is up to you; however, at the very least, you'll need a Question field that will contain questions. You might also want to add fields like Category (the category the question belongs to), Level (difficulty level of the question), DateUsed (last time the question was used), and Notes (miscellaneous notes).

When the database is ready, populate it with questions. Once you have done that, you can start working on the quiz document. Create a blank Writer document and add questions to it:

  • Press F4 to open the Data Source window.
  • Double-click on the created database and then double-click on the Tables folder.
  • Click on the table containing questions. The contents of the table will appear in the window to the right.
  • Select the question you want to add and drag it onto the document.

The next step is to add answer options. Since the quiz contains multiple choice questions, you have to add a group to each question consisting of the correct answer and some incorrect alternatives (they are sometimes called "distractors"). To do this you have to activate toolbars that allow you to add form controls and set their properties. Go to View > Toolbars, then select the Form Controls and Form Design toolbars. On the Form Controls toolbar, click on the More Controls button to access additional form elements. Switch to design mode using the Design Mode On/Off button in the Form Design toolbar. Press then the Group Box button and draw a rectangle in the document right under the question.

This will open the Group Element Wizard, which will guide you through the process of creating the group box. Use the Wizard to add answer options and configure other settings (all of them are self-explanatory). When you click Finish, a new group box containing radio buttons with the defined answers will be inserted in the document. In the same manner you can add group boxes with answers to other questions in the quiz.

Adding interactivity

If you turn off design mode, you can test the form, but there is still some work to be done. First of all, to make the form interactive, you have to add a mechanism that informs you whether the selected answer is correct. You can do this using a simple macro.

Choose Tools > Macros > Organize Macros > OpenOffice.org Basic, double-click on the document's name, and then on the Standard folder. Press the New button, give the new module a name (for example, FormMacros), and type the following code in the macro editor window:

Sub CorrectAnswer
Message="The answer is correct!"
MsgBox Message
End Sub
Sub IncorrectAnswer
Message="The answer is incorrect!"
MsgBox Message
End Sub

The code contains two separate macros (CorrectAnswer and IncorrectAnswer), which display a pop-up window with either the "The answer is correct!" or "The answer is incorrect!" messages. Save the macro and close the editor.

The next step is to assign the appropriate macro to each radio button in the group. To be able to edit each radio button in the group box separately, you have to "ungroup" it first. Select the group box, right-click on it, and choose Edit Group. Select then the first radio button, double-click on it, and choose Control. In the Properties window, click on the Events tab and press the Browse button next to the Item Status Changed field. Press the Assign button and select the appropriate macro -- for example, CorrectAnswer for the radio button representing the correct answer. Repeat this step for other radio buttons in the group. Finally, select the group, right-click on it, and choose Exit Group.

The quiz is almost ready, but like any other form it also needs a Reset button that clears all the answers; you need another simple macro for that. Choose Tools > Macros > Organize Macros > OpenOffice.org Basic, select the FormMacros module, press the Edit button, and Append the following code:

Sub ResetControls
oDoc = thisComponent
oDrawPage = oDoc.getDrawPage()
oForms = oDrawPage.getForms()
oForm = oForms.getByName("Standard")
oForm.Reset()
End Sub

Switch back to the document, make sure that it's in the design mode, press the Button control in the Form Control toolbar, and draw a button in the document. While the button is still selected, right-click on it and choose Control. In the Properties window under the General tab, type "Reset" into the Label field. Click on the Events tab and press the Browse button next to the Mouse Button Released field, and assign the ResetControls macro to the button. Turn the design mode off, and your quiz is ready.

That's it. As you can see, creating an interactive form in Writer is not particularly difficult, and once you know the basics, you can design more advanced documents.

Dmitri Popov is a freelance writer whose articles have appeared in Russian, British, and Danish computer magazines.

Click Here!