PDA

View Full Version : [SOLVED:] Same VB code not working for similar Content Controls with different CC titles



New@VB
04-24-2024, 06:41 AM
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

gmaxey
04-24-2024, 09:24 AM
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

New@VB
04-24-2024, 04:07 PM
This is exactly what I needed!
Thank you very much. I've learned a lot.

gmaxey
04-24-2024, 05:17 PM
Glad to help.