Consulting

Results 1 to 7 of 7

Thread: Auto-populate Text Field When I Click Check Boxes

  1. #1

    Auto-populate Text Field When I Click Check Boxes

    Hi,
    I've been searching for this online for longer than I think it might actually take to solve! I hope someone here can help.

    I'm creating a data collection form in Word 2019 that will have two checkbox sections. I would like to program the form so when I select check boxes from a list of options, the names I assign to the check boxes auto-populate a text field.

    For further information: I'm using content controls for the check boxes and the text field. One set of check boxes will be a group of approximately 20 boxes that will be for different organizational units. I want users to be able to select however many of these organizational units they need to, and when they click the check boxes, I want the names of those org units to appear in the text field. I want the same thing to happen with the second group of check boxes, which will be approximately 80 boxes that will represent different topics the form users can select. The topic names should appear in a second text field (not the same as the org units' text field) when a user clicks the check boxes.

    Is this possible? Do you need additional information?


    Thank you!
    Karen

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    You could try this. Unfortunately there is no change event for content controls and the code won't fire until the user selects and exits the content control. A userform would probably be better for your requirement.

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
      Select Case ContentControl.Title
        Case "A", "B", "C" 'These are the titles of the organization text boxes
          BuildOrgList
        Case "1", "2", "3" 'These are the titles of the 80 group
          'Build80List
      End Select
    lbl_Exit:
      Exit Sub
    End Sub
    
    Sub BuildOrgList()
    Dim oCCOrgs As ContentControls
    Dim oCC As ContentControl
    Dim arrChecked() As String
    Dim lngIndex As Long, lngChecked As Long
    Dim strOrgs As String
      lngChecked = 0
      Set oCCOrgs = ActiveDocument.SelectContentControlsByTag("Org")
      Set oCC = ActiveDocument.SelectContentControlsByTitle("Organizations").Item(1)
      For lngIndex = 1 To oCCOrgs.Count
        If oCCOrgs(lngIndex).Checked Then
          ReDim Preserve arrChecked(lngChecked)
          arrChecked(lngChecked) = oCCOrgs(lngIndex).Title
          lngChecked = lngChecked + 1
        End If
      Next
      On Error GoTo Err_EmptyArray
      Select Case UBound(arrChecked)
        Case 0
          oCC.Range.Text = arrChecked(0)
        Case 1
          oCC.Range.Text = arrChecked(0) & " and " & arrChecked(1)
        Case Is > 1
          For lngIndex = 0 To UBound(arrChecked) - 1
            If lngIndex = 0 Then
              strOrgs = arrChecked(0)
            Else
              strOrgs = strOrgs & ", " & arrChecked(lngIndex)
            End If
          Next lngIndex
          strOrgs = strOrgs & " and " & arrChecked(UBound(arrChecked))
          oCC.Range.Text = strOrgs
      End Select
    lbl_Exit:
      Exit Sub
    Err_EmptyArray:
      oCC.Range.Text = " "
      Resume lbl_Exit
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Thank you for providing this information. I'm afraid I'm not as familiar with VB code as I thought I was. I see where the org unit and topic check box names should go, but I'm not really sure what part of this code refers to the two separate text fields where the check box names will appear. Could you please help me identify those? I thought I had figured it out for the org unit text field, but it didn't work.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Sorry. I was late for an appointment and rushed to answer.

    The list of organizations selected will go in a text content control titled "Organizations." The check boxes for the organization should be titled with the unique organization name and all should have the common tag "Org"

    The code goes in the ThisDocument module of the VB Project.

    I didn't write the procedure for the other group of 80 CCs (just showed a placeholder and suggestion procedure name). It would be done similar to the organizations.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    You're brilliant! Thank you so much. This worked perfectly. I had to create a third list to accommodate the length of my topics, but that worked perfectly as well.

    One more question: Is there a way to program the script to make the checked items appear in alphabetical order in the text field?

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Just insert the following line as shown:

    On Error GoTo Err_EmptyArray
    WordBasic.SortArray arrChecked 'insert this line
    Select Case UBound(arrChecked)
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    Thank you for all your help. You provided me with exactly what I needed. The form works exactly as I was trying to make it work. Thank you, thank you, thank you!

Posting Permissions

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