Microsoft KB Archive/212540

From BetaArchive Wiki
Knowledge Base


WD2000: How to Create Mutually Exclusive Check Boxes by Using a Macro

Article ID: 212540

Article Last Modified on 10/11/2006



APPLIES TO

  • Microsoft Word 2000 Standard Edition



This article was previously published under Q212540


SUMMARY

In Microsoft Word, there is no built-in functionality that allows only one check box out of a group of check boxes to be selected.

For example, if you have three check boxes (for example; Yes, No, and Undecided), you can select more than one check box at a time. However, you may want to allow only one check box to be selected at a time.

This article describes a method of using a Visual Basic for Applications macro that allows the selection of only one check box from a group of check boxes.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

212536 OFF2000: How to Run Sample Code from Knowledge Base Articles


The following sample Visual Basic for Applications macro demonstrates two groups of check boxes on a form. One group contains check boxes whose bookmark names are Yes, No, and Undecided, and the other group contains check boxes whose bookmark names are True and False. When you select one of the check boxes in a group, the other check boxes in that group are cleared.

Sub ToggleCheckBoxOnEntry()

   Dim fFields As FormFields
   Dim fSelectedField As FormField

   Set fFields = ActiveDocument.FormFields

   For Each fSelectedField In Selection.FormFields

      ' Form field must be a check box.
      If fSelectedField.Type = wdFieldFormCheckBox Then

         ' Go to the case for the currently selected group.
         Select Case fSelectedField.Name

            ' Yes, No, or Undecided check box group.
            Case "Yes", "No", "Undecided"

               ' Clear all values.
               fFields("Yes").CheckBox.Value = False
               fFields("No").CheckBox.Value = False
               fFields("Undecided").CheckBox.Value = False

            ' True or False check box group.
            Case "True", "False"

               ' Clear all values.
               fFields("True").CheckBox.Value = False
               fFields("False").CheckBox.Value = False

            ' You can add other Case statements for other groupings.
            ' For example:
            'Case
            '   <Code>
            'Case
            '   <Code>

            Case Else

         End Select

         ' Check the selected formfield.
         fSelectedField.CheckBox.Value = True

      End If

   Next

End Sub
                

To use this macro for any check box grouping within your document, assign ToggleCheckBoxOnEntry() as the Entry macro for every Check Box Form Field in the group. To assign the macro to the Run on entry, follow these steps:

  1. Double-click the form field that you want to add the macro to. This displays the Check Box Form Field Options dialog box.
  2. Under Run macro on, click to select the Entry list. Click to select ToggleCheckBoxOnEntry from the list of macros, and then click OK.
  3. Repeat these steps for each of the check box fields.
  4. Protect the document by clicking the Lock button on the Forms toolbar. Or, on the Tools menu, click Protect Document, click to select Forms and then click OK.


REFERENCES

For more information about Case statements, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type Select Case statement in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For additional information about creating online forms, click the article number below to view the article in the Microsoft Knowledge Base:

212328 WD2000: How to Create an Online Form Using Form Fields


For additional information about getting help with Visual Basic for Applications, click the article numbers below to view the articles in the Microsoft Knowledge Base:

212623 WD2000: Macro Programming Resources


226118 OFF2000: Programming Resources for Visual Basic for Applications



Additional query words: vb vba vbe

Keywords: kbdtacode kbhowto kbmacroexample KB212540