PDA

View Full Version : Need to change value of checkbox from userform



magsol76
12-03-2020, 03:30 AM
I have a userform where i want checkboxes from a worddocument to be controlled from a userform. But i feel stupid since i can´t even find out the names of the checkboxes from the worddocument.
They have been added from the developertab and are active x controls type checkbox. I can check the properties of the checkbox from the developertab but the name of the checkbox is nowhere to be found.

Edit
My bad, figured it out; had selected the wrong kind of checkbox...

gmayor
12-03-2020, 09:45 PM
ActiveX check boxes are best avoided. I don't know what you settled on, but the best choice would be content controls which are simple enough to process.

magsol76
12-04-2020, 08:02 AM
It seems i have used checkbox content controls all over the document so if i could get them to work the better.
How would i go about doing it? I have 2 checkboxes to start with that I have given the tag CC_ja and CC_nej respectively.

The If-statement that controls the checkboxes is:

Private Sub Generera_Click()

If ActiveDocument.Bookmarks.Exists("bookmarkname") = False Then
???CC_ja.Checked = True
???CC_nej.Checked = False
Else
???CC_ja.Checked = False
???CC_nej.Checked = True
End If

End Sub

I have no clue what i should write to use CC_ja and CC_nej from the active document as I have never called content controls from a userform.

gmaxey
12-04-2020, 01:23 PM
ActiveDocument.SelectConentControlsByTitle("???CC_ja").Item(1).Checked = True
ActiveDocument.SelectConentControlsByTitle("???CC_nea").Item(1).Checked = False

magsol76
12-04-2020, 02:32 PM
I tried this code (tried another if-statement) but it can´t find the contentcontrol. I get error 5941.
PS. I use tags instead of title.

If UserForm1.OB_Fastighetsägare_Ja.Value = True And (UserForm1.OB_Verksamhet_Företag.Value = True Or UserForm1.OB_Verksamhet_Lantbruk.Value = True) Then
ActiveDocument.SelectContentControlsByTag("CC_ja.Value").Item(1).Checked = True
ActiveDocument.SelectContentControlsByTag("CC_nej.Value").Item(1).Checked = False
End If

magsol76
12-04-2020, 02:41 PM
Found the fault with the .Value from the previous code. This is my new code and it works fine:

If UserForm1.OB_Fastighetsägare_Ja.Value = True And (UserForm1.OB_Verksamhet_Företag.Value = True Or UserForm1.OB_Verksamhet_Lantbruk.Value = True) Then
ActiveDocument.SelectContentControlsByTag("CC_ja").Item(1).Checked = True And ActiveDocument.SelectContentControlsByTag("CC_nej").Item(1).Checked = False
End If

If UserForm1.OB_Fastighetsägare_Nej.Value = True And (UserForm1.OB_Verksamhet_Företag.Value = True Or UserForm1.OB_Verksamhet_Lantbruk.Value = True) Then
ActiveDocument.SelectContentControlsByTag("CC_ja").Item(1).Checked = False And ActiveDocument.SelectContentControlsByTag("CC_nej").Item(1).Checked = True
End If

If UserForm1.OB_Fastighetsägare_Vetej.Value = True And (UserForm1.OB_Verksamhet_Företag.Value = True Or UserForm1.OB_Verksamhet_Lantbruk.Value = True) Then
ActiveDocument.SelectContentControlsByTag("CC_ja").Item(1).Checked = False And ActiveDocument.SelectContentControlsByTag("CC_nej").Item(1).Checked = False
End If