Thread: [Solved] Check Box Macro
View Single Post
 
Old 04-05-2011, 06:28 PM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Johnny,

You could use an 'on exit' macro attached to the 'select all' checkbox, coded like:
Code:
Sub CheckAll()
Dim FrmFld As FormField, bChkAll As Boolean
With ActiveDocument
  bChkAll = .FormFields("CheckAll").Result
  For Each FrmFld In .FormFields
    If FrmFld.Type = wdFieldFormCheckBox Then FrmFld.Result = bChkAll
  Next
End With
End Sub
Note: The above code assumes your 'select all' checkbox has the bookmark name 'CheckAll' assigned to it. Checking the box checks all checkboxes; unchecking it unchecks them.

For something a little more sophisticated, you could use an 'on entry' macro as well to test the state of the checkbox before it is clicked, with code like the 'GetChkState' below:
Code:
Dim bChkAll As Boolean
 
Sub CheckAll()
Dim FrmFld As FormField
With ActiveDocument
  If .FormFields("CheckAll").Result = bChkAll Then Exit Sub
  bChkAll = .FormFields("CheckAll").Result
  For Each FrmFld In .FormFields
    If FrmFld.Type = wdFieldFormCheckBox Then FrmFld.Result = bChkAll
  Next
End With
End Sub
 
Sub GetChkState()
bChkAll = ActiveDocument.FormFields("CheckAll").Result
End Sub
With this approach, the checkboxes will only be updated if the checked state of the 'select all' checkbox has changed. This might help prevent some accidental updates. Note that the 'CheckAll' sub has been modified to suit and that the 'bChkAll' variable is now module-wide.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote