"Check All" checkbox

G

Gavin

Hi all

I am trying to create a 'check all' checkbox. I have several hundred
checkboxes and want to be able to click one master checkbox and have them all
be checked with one click. I don't want to set them all to be checked
initially though.
I thought it might be possible with a loop statement. Any ideas?

Gav
 
R

Russ

Gav,

Unless you need to visually see all checkboxes with checkmarks in them, you
could just have the one special checkbox be an "All the Above (or Below)"
type of checkbox and adjust your subsequent code to test if that special
checkbox was checked by the user or not.

Otherwise, yes a loop would be used to checkmark them all and a similar loop
would be used to test which were checkmarked.

In formfield checkboxes within a Document, you might use code like this to
look at the FormFields collection:

Public Sub Put_Check_In_All_Doc_Boxes()
Dim Ffield As Object

For Each Ffield In FormFields
If ActiveDocument.FormFields(Ffield).Type = wdFieldFormCheckBox Then
ActiveDocument.FormFields(Ffield).CheckBox.Value = True
End If
Next Ffield
End Sub

UserForm checkboxes may test the Controls collection like this:
(if checkboxes are named checkbox1,checkbox2,etc. and Userform is named
UserForm1)
Public Sub Put_Check_In_All_UserForm_Boxes()
Dim myControl As Object

For Each myControl In UserForm1.Controls
If Control.Name Like "Checkbox*" Then
Control.Value = True
End If
Next myControl
End Sub
 
D

Doug Robbins - Word MVP

What type of checkboxes? FormField Checkboxes or checkboxes on a userform?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

Russ

Oops, correction below
Gav,

Unless you need to visually see all checkboxes with checkmarks in them, you
could just have the one special checkbox be an "All the Above (or Below)"
type of checkbox and adjust your subsequent code to test if that special
checkbox was checked by the user or not.

Otherwise, yes a loop would be used to checkmark them all and a similar loop
would be used to test which were checkmarked.

In formfield checkboxes within a Document, you might use code like this to
look at the FormFields collection:

Public Sub Put_Check_In_All_Doc_Boxes()
Dim Ffield As Object

For Each Ffield In FormFields
If ActiveDocument.FormFields(Ffield).Type = wdFieldFormCheckBox Then
ActiveDocument.FormFields(Ffield).CheckBox.Value = True
End If
Next Ffield
End Sub

UserForm checkboxes may test the Controls collection like this:
(if checkboxes are named checkbox1,checkbox2,etc. and Userform is named
UserForm1)
Public Sub Put_Check_In_All_UserForm_Boxes()
Dim myControl As Object

For Each myControl In UserForm1.Controls
If Control.Name Like "Checkbox*" Then
Control.Value = True
The above two lines should be:
IF myControl.Name Like "Checkbox*" Then
myControl.Value = True
 
G

Gavin

Thanks Guys,

I am a bit new to this VB thing so I don't have a real good grasp yet.
I am using the control toolbar to insert the checkboxes directly into the
document - should I be using insert>>userform in VB and will this then make
your sample code work?
 
R

Russ

Gav,

The first subroutine of my sample code will work with what you are doing
(checkboxes in documents).
You can use this type of form especially when you want to print out what the
user's choices were. But by testing what was checkmarked, you can also
decide what else should appear in the document.

The second subroutine is for checkboxes in Userforms, which are like home
made popup dialog windows to interact with the writer and his document. The
forms are normally ephemeral, since they usually are not modeless. (not
modeless means that only the userform will accept input while it is on
screen) They are made to disappear after the user makes a decision and
subsequent actions are made on the document based on those decisions. I,
myself, am more familiar with them at this time.
 
G

Gavin

I can't make any of this work. I think I need to get a better understanding
of VB before I can work this one out.

Thanks for your help though.
 
D

Doug Robbins - Word MVP

The check box entered from the Control Toolbar is an ActiveX control, not a
formfield control, hence your code will not work with it.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

Russ

Doug,
Since it is an ActiveX Control Toolbar, does that mean that MacWord doesn't
have anything comparable? Or are you just talking about inserting a html
checkbox object into the document.
 
J

Jean-Guy Marcil

Gavin was telling us:
Gavin nous racontait que :
I can't make any of this work. I think I need to get a better
understanding of VB before I can work this one out.

Since you are using ActiveX controls, double click on your "Check All" check
box to access its code pane.

Paste this code between the
Private Sub CheckBoxName_Click()
and the
End Sub
lines:
(Replacing CheckBoxName by the actual name you have)

'_______________________________________
Dim myObj As Field
Dim ObjNames As String
Dim SourceDoc As Document

Set SourceDoc = ActiveDocument

For Each myObj In SourceDoc.Fields
With myObj
If InStr(1, .Code, "Forms.CheckBox.1") > 0 Then
.OLEFormat.Object.Value = CheckBox4.Value
End If
End With
Next myObj
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top