PDA

View Full Version : [SOLVED:] Visibility of content controls



New@VB
04-09-2024, 04:37 PM
I have a Word document (aaa.docm) that contains two content controls. The first is a drop-down list content control (Title: CCDDL) with the selections "No issues" and "Issues found". The second is a text box content control (Title: CCTB) to be used to describe the "Issue". I would like the Content Control Text Box to disappear if "No issues" is selected from the drop-down list.  When I use the below VB code, the Content Control Text Box will disappear if I select "No Issue" but it will not reappear if I select "Issues found".  What am i doing wrong?


Sub UpdateTextBoxVisibility()
Dim ddValue As String
ddValue = ActiveDocument.SelectContentControlsByTitle("CDDL")(1).DropdownListEntries(1).Value
If ddValue = "No issues" Then
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.Hidden = True
Else
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.Hidden = False
End If
End Sub

Thanks for your help!

Aussiebear
04-09-2024, 08:04 PM
Welcome to VBAX New@VB

Try changing the logic around


Sub UpdateTextBoxVisibility()
Dim ddValue As String
ddValue = ActiveDocument.SelectContentControlsByTitle("CDDL")(1).DropdownListEntries(1).Value
If ddValue <> "No issues" Then
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.Hidden = False
Else
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.Hidden = True
End If
End Sub

arnelgp
04-09-2024, 08:34 PM
content controls don't have events of their own.

New@VB
04-10-2024, 07:33 AM
Aussiebear - Thanks... I tried as you suggested but still can't get it to work.
arnelg - thanks... but i don't understand what you mean by content controls don't have events of their own. Is there additional code that i need to add to have it function?

georgiboy
04-10-2024, 08:10 AM
I noticed in your code you had used 'CDDL' as the name, however, you stated that the name was 'CCDDL'.
Secondly, from my understanding 'DropdownListEntries(1).Value' will always be the same value as it is just the first item in the list.

The below code will fire when you change the CCDDL value and exit the content control:


Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim ddValue As String
ddValue = ActiveDocument.SelectContentControlsByTitle("CCDDL")(1).Range.Text

If ddValue = "No issues" Then
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.Hidden = True
Else
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.Hidden = False
End If
End Sub

gmaxey
04-14-2024, 04:18 PM
Sub UpdateTextBoxVisibility()
If ActiveDocument.SelectContentControlsByTitle("CCDDL")(1).Range.Text = "Issues" Then
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.Hidden = False
Else
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.Hidden = True
End If
End Sub

New@VB
04-22-2024, 11:49 AM
Still doesn't work. I must be still be doing something wrong. I've started an new file that only contains these some basic text and these two content controls. The file was saved as a .docm

Drop down options in CCDDL are: "Issues found" and "No issues".



Sub UpdateTextBoxVisibility()
If ActiveDocument.SelectContentControlsByTitle("CCDDL")(1).Range.Text = "Issues found" Then
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.Hidden = False
Else
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.Hidden = True
End If
End Sub


Also, should the code be entered into the "Module" or "Class Module" window?
Thanks!!!
Paul.

Aussiebear
04-22-2024, 01:18 PM
In a Worksheet Module

New@VB
04-22-2024, 03:40 PM
I cant find "Worksheet Module". FYI - I'm using this in a Word document.

Aussiebear
04-22-2024, 07:32 PM
My apologies. Document Module.

gmaxey
04-23-2024, 05:32 AM
Can you post your sample document. There is nothing wrong with your code. What is calling your code?

New@VB
04-23-2024, 07:47 AM
How do I post my document?
Thanks!

georgiboy
04-23-2024, 08:00 AM
Same as you have created the post above except click 'Go Advanced'. Then you can see the last link in my signature below which should show you the rest.

New@VB
04-23-2024, 09:06 AM
Attached is the sample file that I am having problems with configuring: 1) visibility of a textbox content control based on the selection from a content control dropdown; and, 2) to change the color of the font based on the dropdown menu.
Your assistance is greatly appreciated!
I have learned a lot already! Thanks!

gmaxey
04-23-2024, 01:55 PM
I didn't see anywhere that you were calling the code to set your visibility. "Hidden" text is a poor method, because some people like to show hidden text in documents. Put this is the ThisDocument module:


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
Case Else
.Font.ColorIndex = wdBlack
End Select
End With
'UpdateTextBoxVisibilityI
'Or
'UpdateTextBoxVisibilityII
'Or
UpdateTextBoxVisibilityIII
End If
End Sub

Sub UpdateTextBoxVisibilityI()
If ActiveDocument.SelectContentControlsByTitle("CCDDL")(1).Range.Text = "Issues found" Then
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.Hidden = False
Else
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.Hidden = True
'Note: if you or your users has File>Options>Display>Show Hidden Text checked then you will see the text with a dotted line under it.
'You might want to use as white font, or set the placeholder text to a zero length string.
End If
End Sub

Sub UpdateTextBoxVisibilityII()
If ActiveDocument.SelectContentControlsByTitle("CCDDL")(1).Range.Text = "Issues found" Then
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.ColorIndex = wdBlack
Else
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Font.ColorIndex = wdWhite
End If
End Sub

Sub UpdateTextBoxVisibilityIII()
If ActiveDocument.SelectContentControlsByTitle("CCDDL")(1).Range.Text = "Issues found" Then
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).SetPlaceholderText , , "Enter issues here"
Else
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).SetPlaceholderText , , ChrW(8203)
ActiveDocument.SelectContentControlsByTitle("CCTB")(1).Range.Text = vbNullString
End If
End Sub

New@VB
04-23-2024, 02:59 PM
Solved
Fantastic!!! I like the "UpdateTextBoxVisibilityIII" the best!
Thanks for all that helped. I continue to learn and having fun!
Cheers!

gmaxey
04-24-2024, 02:51 AM
So if you wished, you could simply use:


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

New@VB
04-24-2024, 06:45 AM
Even better! I'll use this. I've posted another issue in a separate thread: why I can't get this code to work for similar CCtls with different names.
Thanks again!