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
Related
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
I am creating a program which has several UserForms.
At the end of the program I need to clear every Checkbox inside some UserForm. I have created a Function, but it cannot recognise which UserForm it should clear, can you help me there? Here is the code:
Function ClearUserForm(ByVal userf As String)
Dim contr As Control
For Each contr In userf.Controls
If TypeName(contr) = "CheckBox" Then
contr.Value = False
End If
Next
End Function
And I am calling the function like this, for example:
ClearUserForm ("UserForm2")
It seems not to recognize which UserForm it should act upon.
Shai Rado's advice is good and you should have a look at how he creates the object from its 'key'.
I only post this answer to check if you're aware that you could pass the object itself in the call. So your code could be like so:
Option Explicit
Public Sub RunMe()
ClearCBoxes UserForm1
End Sub
Private Sub ClearCBoxes(frm As MSForms.UserForm)
Dim ctrl As Control
For Each ctrl In frm.Controls
If TypeOf ctrl Is MSForms.ComboBox Then
ctrl.Value = False
End If
Next
End Sub
You don't need a Function (since you are not returning any arguments), in your case a Sub will do.
You need to qualify an Object to the User_Form selected by using:
Set objUserForm = UserForms.Add(userf)
Whole code
(Tested)
Option Explicit
Sub ClearUserForm(ByVal userf As String)
Dim contr As Control
Dim objUserForm As Object
Set objUserForm = UserForms.Add(userf)
For Each contr In objUserForm.Controls
If TypeName(contr) = "CheckBox" Then
contr.Value = False
End If
Next
' just to check that all checkboxes are cleared
objUserForm.Show
End Sub
I have some Forms Checkboxes in Excel 2010. I need to perform some common code when they are clicked. To do this, I'd like to pass a reference to the Checkbox, but so far I'm only able to get it typed as a shape.
To preempt the question, yes, they need to be Form Checkboxes and not ActiveX Checkboxes.
I'm a novice with VBA, so any help is appreciated.
Sub CheckBox1_Click()
'I really want this reference to be a Checkbox, not a Shape
Dim shape As Shape
Set shape = ActiveSheet.Shapes("Check Box 1")
DoSomething(shape)
End Sub
Sub DoSomething(MSForms.CheckBox)
'I need the reference to be a checkbox as I need to check
'whether it's checked or not here
End Sub
In such a scenario, don't have different click event for all checkboxes. Have just one. And use Application.Caller to get the name of the ckeckbox which called it. Pass that as a String to the relevant sub and then work with it.
UNTESTED
Sub CheckBoxMain_Click()
Dim sName As String
sName = Application.Caller
DoSomething (sName)
End Sub
Sub DoSomething(sCheck As String)
Dim shp As shape
Set shp = ActiveSheet.Shapes(sCheck)
With shp
'~~> Do something
End With
End Sub
You could also combine the two into one as well and link it with all checkboxes.
Sub DoSomething()
Dim shp As shape
Set shp = ActiveSheet.Shapes(Application.Caller)
With shp
'~~> Do something
End With
End Sub
This is similar to Siddharth's but adds the ControlFormat property of the Shape. ControlFormat gets you the Intellisense for the CheckBox, in this case Value:
Sub CheckBox1_Click()
Dim chk As Shape
Set chk = ActiveSheet.Shapes(Application.Caller)
With chk.ControlFormat
If .Value = True Then
MsgBox "true"
Else
MsgBox "false"
End If
End With
End Sub
I am trying to write some code for a VBA userform that has about 100 checkboxes. I was wondering if there is a way that I could have one piece of code that applies to any checkbox or if I have to write 100 seperate functions for checkbox1_click, checkbox2_click, checkbox3_click, etc.
Thanks for any help in advance :)
edit: I realized that it would help to explain exactly what I am trying to do. There will be 100 check boxes and whenever one is clicked I would like to do this:
Call CheckBoxClicked("checkboxname")
Put this in a class moduled named clsCheckBoxHandler
Public WithEvents chk As MSForms.CheckBox
Private Sub chk_Click()
MsgBox chk.Caption & " Clicked!"
End Sub
then in the Userform
Dim chkCollection As Collection
Private Sub UserForm_Initialize()
Dim cCont As Control
Dim chkH As clsCheckBoxHandler
Set chkCollection = New Collection
For Each cCont In Me.Controls
If TypeName(cCont) = "CheckBox" Then
Set chkH = New clsCheckBoxHandler
Set chkH.chk = cCont
chkCollection.Add chkH
End If
Next cCont
End Sub
this is just a simple handler for checkboxes that has a click event but can be extended to cover multiple controls and events.
I have a word doc with several checkbox fields. I can fill the text fields, but haven't figured out how to check checkboxes.
I can't just make a macro in word and see how word does it because to use the keyboard to check the box (space bar), you have to enable document protection, which disables macro creation.
I just tried setting an old fashion Word2003 Checkbox with VBA. Worked with that piece of code:
' demo purposes - added a command
Private Sub CommandButton1_Click()
' FormFields refers to Word2003 FormFields
If ActiveDocument.FormFields(1).Type = wdFieldFormCheckBox Then
ActiveDocument.FormFields(1).CheckBox.Value = True
End If
' ContentControls refers to >= Word2007 Controls - thx to StevenDotNet for the hint
ActiveDocument.ContentControls(1).Checked = True
End Sub
On the other hand I created a VS2012 WordProject with VB.net and added some code to check the box on load.
Private Sub ThisDocument_Open() Handles Me.Open
Me.FormFields(1).CheckBox.Value = True
End Sub
Each checkbox has a Value property. You can set this property to True or False to check or uncheck the checkbox.
EDIT
I wrote a little macro that ticks all boxes in the active document. You can edit it to suit your needs. VBA is really nasty though, it took me around 15 minutes to figure this out.
Sub CheckAllBoxes()
Dim ctrl As ContentControl
For Each ctrl In ActiveDocument.ContentControls
If ctrl.Type = wdContentControlCheckBox Then
ctrl.Checked = True
End If
Next
End Sub