Consulting

Results 1 to 4 of 4

Thread: Same VB code not working for similar Content Controls with different CC titles

  1. #1
    VBAX Regular
    Joined
    Apr 2024
    Posts
    10
    Location

    Same VB code not working for similar Content Controls with different CC titles

    I have a Word document (attached) where the user is to enter information about the status of several tasks. For each task the user is to select the status of the task from a content control dropdown list. Options are: "No issues" or "Issues found". If "Issues found" is selected, the user is to provide a description of the issue for that task in a textbox.

    While the code works for the first task, it does not work for the content controls in the second task

    What am I doing wrong?

    Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
      If CCtrl.Title = "CCDDL" Then
         With CCtrl.Range
           Select Case .Text
              Case "Issues found"
                .Font.ColorIndex = wdRed
                 ActiveDocument.SelectContentControlsByTitle("CCTB")(1).SetPlaceholderText , , "Enter issues here"
              Case Else
                .Font.ColorIndex = wdBlack
                ActiveDocument.SelectContentControlsByTitle("CCTB")(1).SetPlaceholderText , , ChrW(8203)
                ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Text = vbNullString
           End Select
         End With
      End If
    End Sub
    
    
    Private Sub Document_ContentControlOnExit2(ByVal CCtrl As ContentControl, Cancel As Boolean)
      If CCtrl.Title = "CCDDL2" Then
         With CCtrl.Range
           Select Case .Text
              Case "Issues found"
                .Font.ColorIndex = wdRed
                 ActiveDocument.SelectContentControlsByTitle("CCTB2")(1).SetPlaceholderText , , "Enter issues here"
              Case Else
                .Font.ColorIndex = wdBlack
                ActiveDocument.SelectContentControlsByTitle("CCTB2")(1).SetPlaceholderText , , ChrW(8203)
                ActiveDocument.SelectContentControlsByTitle("CCTB2")(1).Range.Text = vbNullString
           End Select
         End With
      End If
    End Sub
    Attached Files Attached Files

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,341
    Location
    There is no such thing as "Document_ContentControlOnExit2(ByVal CCtrl As ContentControl, Cancel As Boolean)" The procedure below is the event that is triggered when you exit a content control. You have to use it for one, two or a million CCs.

    Sub Document_ContentControlOnExit2(ByVal CCtrl As ContentControl, Cancel As Boolean)
    Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
      With CCtrl
        Select Case .Title
          Case "CCDDL"
             With .Range
                Select Case .Text
                   Case "Issues found"
                     .Font.ColorIndex = wdRed
                      ActiveDocument.SelectContentControlsByTitle("CCTB")(1).SetPlaceholderText , , "Enter issues here"
                   Case Else
                     .Font.ColorIndex = wdBlack
                     ActiveDocument.SelectContentControlsByTitle("CCTB")(1).SetPlaceholderText , , ChrW(8203)
                     ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Text = vbNullString
                End Select
             End With
          Case "CCDDL2"
            With .Range
                Select Case .Text
                   Case "Issues found"
                     .Font.ColorIndex = wdRed
                      ActiveDocument.SelectContentControlsByTitle("CCTB2")(1).SetPlaceholderText , , "Enter issues here"
                   Case Else
                     .Font.ColorIndex = wdBlack
                     ActiveDocument.SelectContentControlsByTitle("CCTB2")(1).SetPlaceholderText , , ChrW(8203)
                     ActiveDocument.SelectContentControlsByTitle("CCTB2")(1).Range.Text = vbNullString
                End Select
             End With
        End Select
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Apr 2024
    Posts
    10
    Location
    This is exactly what I needed!
    Thank you very much. I've learned a lot.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,341
    Location
    Glad to help.
    Greg

    Visit my website: http://gregmaxey.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
  •