Consulting

Results 1 to 10 of 10

Thread: Content control checkbox used to populate text fields

  1. #1
    VBAX Regular
    Joined
    Nov 2015
    Posts
    14
    Location

    Content control checkbox used to populate text fields

    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.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Nov 2015
    Posts
    14
    Location
    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?

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Unless I was seeing things, the code I provided above already does that.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular
    Joined
    Nov 2015
    Posts
    14
    Location
    Ah i see I was not reading that correctly. Thanks so much for the help!

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    You're welcome.
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Regular
    Joined
    Nov 2015
    Posts
    14
    Location
    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

  8. #8
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  9. #9
    VBAX Regular
    Joined
    Nov 2015
    Posts
    14
    Location
    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.

  10. #10
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •