Word VB, Clear all checked fields - vba

i have a word with 15+ VB forms and 20-50 CheckBoxes in each.
How do i clear (if they are cheched) all the CheckBoxes in active form w/o having to write the name of each CheckBox?
Thank you

This code will do the job. It must be placed in the UserForm's code sheet.
Private Sub ClearCheckBoxes()
Dim Ctl As MSForms.Control
For Each Ctl In Me.Controls
If TypeName(Ctl) = "CheckBox" Then Ctl.Value = False
Next Ctl
End Sub

Related

VBa to clear all activex checkboxes in Word document

I'm trying to create a button to clear all the activex check-boxes in a .docm file, but nothing seems to work. I saw many posts in the fórum, but none seems to work. Can anyone help me? Obs.: the original file have at least 30 check boxes and I don't want to type all the check-boxes names.
See the code below and the example file attached
Ps.: I tried ThisDocument.Fields; Me.Control and many others.
See attached image as example: ActiveX CheckBox
Private Sub CommandButton1_Click()
Dim Ctl As MSForms.Control
For Each Ctl In ThisDocument.FormFields
If Ctl.Name = "CheckBox" Then Ctl.Value = False
Next Ctl
End Sub
After many tests, the code below solved my problem:
Private Sub Limpar_Click()
Dim iShp As InlineShape
For Each iShp In ActiveDocument.InlineShapes
If iShp.OLEFormat.ClassType = "Forms.CheckBox.1" Then
iShp.OLEFormat.Object.Value = False
End If
Next
End Sub

Perform a macro by Checkbox Input

I have Check-Boxes with caption of country Names and a OK button on User-form,
User will Mark required Check-Boxes and click OK to submit the form.
Expected Result: For each checked Box there is a Macro to perform.
How do I make OK button to perform macro on Selected Countries which are Check-Marked by User?
and
Is the following code correctly handle the situation? or there is other way of doing that?
If ActiveDocument.CeemeaFinallist.EasternEurope("CheckBox1").CheckBox.Value = True Then
Application.Run MacroName:="Normal.NewMacros.CEEMEA2"
Else
End If
How do I Select all Check-Boxes at once?
Try iterating over the controls and firing the macro where the checkbox is set to True:
Private Sub CommandButton1_Click()
Dim ctl As Control
Dim j As Long
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Me.Controls(ctl.Name).Value = True Then
' Fire macro with ctl.Caption to identify the country
End If
End If
Next
End Sub

Setting same SelectedIndex value for multiple combobox

I have a form where i have 23 Comboboxes. Writing SelectedIndex=-1 for every combobox will definitely work but i want to know whether there's any other way of doing it as done in below given example. ?
For Each ctl As Control In (GroupBox1.Controls)
If TypeOf ctl Is TextBox Then
ctl.Text = ""
End If
Next
i tried this,
If TypeOf ctl Is ComboBox Then
ctl.selectedIndex=-1
End If but it doesn't works. Please help me out.
In your example your ctl variable is a Control and not a Combobox so it does not have the SelectIndex property - though you could have casted it back with DirectCast(ctl, Combobox).
For Each ctl As Control In (GroupBox1.Controls)
If TypeOf ctl Is Combobox Then
DirectCast(ctl, Combobox).SelectedIndex = -1
End If
Next
Or create a loop of type specific control for your loop. No need to check type here.
Dim cbs = GroupBox1.Controls.OfType(Of Combobox)()
For Each cb In cbs
cb.SelectedIndex = -1
Next

How to change multiple comboboxes with little code

Here's my problem.
I have a userform with 10 comboBoxes.
Instead of Select Case al those comboboxes one by one is
there a smart way to change multiple combobox properties such as backcolor or fontcolor?
So for example:
Sub ChangeMultipleComboBoxes()
Dim comboB as control
For Each comboB In Me.Controls
If TypeName(comboB) = "ComboBox" Then
comboB.BackColor = vbRed
End If
Next comboB
End Sub
The problem is that for comboB I can't seem to find any properties for changing it's backcolor or whatsoever.
Can someone help me please?
As Zaider says, your code works. You just don't get the IntelliSense for the ComboBox-specific properties when it's declared as a Control, even though you can use the properties as you've done in your code. (Note that properties common to all controls, e.g., Caption are available.)
To get the Intellisense for all ComboBox properties, re-declare the Control as a ComboBox once you've determined that's what it is:
Private Sub UserForm_Click()
Dim ctl As MSForms.Control
Dim comboB As MSForms.ComboBox
For Each ctl In Me.Controls
If TypeName(ctl) = "ComboBox" Then
're-declare it as a ComboBox
Set comboB = ctl
'Now you get IntelliSens
comboB.BackColor = vbRed
End If
Next ctl
End Sub

Toggle checkboxes in a tab based upon master checkbox selection

I'm having issues with an onclick event for a checkbox in a vba form. Basically what I am trying to do is ammend the value of all checkboxes on specific tab to be the same value as a master checkbox. In this instance, it is the 'Use Online' captioned checkbox below (online_toggle in the code) which once clicked should toggle the other checkboxes on the tab 'on' or 'off'. I currently have the following code but it keeps producing an error at 'For Each obj In online.OLEObjects'
Private Sub online_toggle_Click()
Dim ctl As Control
For Each ctl In Me.MultiPage1.Pages(6).Controls
If TypeOf ctl Is MSForms.CheckBox Then
If ctl.GroupName = "online_variants" Then
If ctl.Name <> "online_toggle" Then
ctl.Value = online_toggle.Value
End If
End If
End If
Next ctl
End Sub
N.B. online is the name of the tab on which all of the checkboxes are situated. If it helps the checkboxes affected by the master checkbox are all grouped as online_variants
Cheers,
Jason
In a Mutipage the page numbering starts from 0 so if you are trying to refer to the Checkboxes in the Online tab (7th Tab) then use this
Dim ctl As Control
For Each ctl In Me.MultiPage1.Pages(6).Controls
If TypeOf ctl Is MSForms.CheckBox Then
'~~> Your code here
Debug.Print ctl.Name
End If
Next