PDA

View Full Version : Content control checkbox used to populate text fields



limdul
11-12-2015, 02:55 PM
Hi Everyone,

I am wondering if there is a way to make content control checkboxes automatically populate text into content control text fields. So for example I have 12 text fields that I would like to show N/A if a checkbox is checked. I would also like to see if there is a way to write some script to have it when the check box is unchecked that the N/A goes away. That way someone does not have to fill in N/A multiple times or delete multiple lines.

Also along that same note is there a way to have a check box when checked choose a certain item from a drop down list content control?

Thanks for anyone that can help with this.

gmaxey
11-12-2015, 03:01 PM
Code similar to the following in the ThisDocument module:


Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Select Case ContentControl.Title
Case "Checkbox" 'The title of your CC.
If ContentControl.Checked Then
ActiveDocument.SelectContentControlsByTitle("Text1").Item(1).Range.Text = "N/A"
ActiveDocument.SelectContentControlsByTitle("Text2").Item(1).Range.Text = "N/A"
'And so one
ActiveDocument.SelectContentControlsByTitle("Dropdown1").Item(1).DropdownListEntries.Item(2).Select
Else
ActiveDocument.SelectContentControlsByTitle("Text1").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("Text2").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("Dropdown1").Item(1).DropdownListEntries.Item(1).Select
End If
End Select
End Sub

limdul
11-12-2015, 04:51 PM
Thanks for answering so quickly. One question that I do have though is how can I type the code so that way if I uncheck the checkbox it would clear the textboxes so that it would not have the N/A anymore?

gmaxey
11-13-2015, 06:44 AM
Unless I was seeing things, the code I provided above already does that.

limdul
11-13-2015, 08:08 AM
Ah i see I was not reading that correctly. Thanks so much for the help!

gmaxey
11-13-2015, 08:44 AM
You're welcome.

limdul
11-13-2015, 06:02 PM
Ok so I have another question about this issue. Is there a way to have this code do a range of textboxes?? Because if you see below my code is going to be extremely long if I have to type out each textbox and dropdown box. This is just for one section. I have another section that ranges from textbox21 - textbox66. If there is not a way to condense this it is fine. I will just have a lot of typing to do LOL.

Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
Select Case ContentControl.Title
Case "checkbox1" 'The title of your CC.
If ContentControl.Checked Then
ActiveDocument.SelectContentControlsByTitle("textbox1").Item(1).Range.Text = "N/A"
ActiveDocument.SelectContentControlsByTitle("textbox2").Item(1).Range.Text = "N/A"
ActiveDocument.SelectContentControlsByTitle("textbox3").Item(1).Range.Text = "N/A"
ActiveDocument.SelectContentControlsByTitle("textbox4").Item(1).Range.Text = "N/A"
ActiveDocument.SelectContentControlsByTitle("textbox5").Item(1).Range.Text = "N/A"
ActiveDocument.SelectContentControlsByTitle("textbox6").Item(1).Range.Text = "N/A"
ActiveDocument.SelectContentControlsByTitle("textbox7").Item(1).Range.Text = "N/A"
ActiveDocument.SelectContentControlsByTitle("textbox8").Item(1).Range.Text = "N/A"
ActiveDocument.SelectContentControlsByTitle("textbox9").Item(1).Range.Text = "N/A"
ActiveDocument.SelectContentControlsByTitle("textbox10").Item(1).Range.Text = "N/A"
ActiveDocument.SelectContentControlsByTitle("textbox11").Item(1).Range.Text = "N/A"
ActiveDocument.SelectContentControlsByTitle("textbox12").Item(1).Range.Text = "N/A"
Else
ActiveDocument.SelectContentControlsByTitle("textbox1").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("textbox2").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("textbox3").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("textbox4").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("textbox5").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("textbox6").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("textbox7").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("textbox8").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("textbox9").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("textbox10").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("textbox11").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("textbox12").Item(1).Range.Text = vbNullString
End If
End Select

gmaxey
11-13-2015, 06:25 PM
Select Case ContentControl.Title
Case "checkbox1" 'The title of your CC.
For lngIndex = 1 To 12
If ContentControl.Checked Then
ActiveDocument.SelectContentControlsByTitle("textbox" & lngIndex).Item(1).Range.Text = "N/A"
Else
ActiveDocument.SelectContentControlsByTitle("textbox" & lngIndex).Item(1).Range.Text = vbNullString
End If
Next lngIndex
End Select

limdul
11-17-2015, 12:35 PM
Thank you for your help. Now I have another question. How would I use the code that you have above to do multiple ranges. An example: "For lngIndex = 209 To 214" but then I would like to do 249 - 254 and 297 - 299.

Again thanks for all your help.

gmayor
11-17-2015, 10:27 PM
You can add another case selection e.g.


Select Case ContentControl.Title
Case "checkbox1" 'The title of your CC.
For lngIndex = 1 To 299
On Error Resume Next 'ensures missing numbers do not force an error
Select Case lngIndex
Case 1 To 12, 209 To 214, 249 To 254, 297 To 299 'The groups of numbers separated by commas
If ContentControl.Checked Then
ActiveDocument.SelectContentControlsByTitle("textbox" & lngIndex).Item(1).Range.Text = "N/A"
Else
ActiveDocument.SelectContentControlsByTitle("textbox" & lngIndex).Item(1).Range.Text = vbNullString
End If
End Select
Next lngIndex
Case Else
End Select