Consulting

Results 1 to 18 of 18

Thread: Visibility of content controls

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

    Visibility of content controls

    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!

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    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
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    content controls don't have events of their own.

  4. #4
    VBAX Regular
    Joined
    Apr 2024
    Posts
    10
    Location
    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?

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    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
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

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

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Regular
    Joined
    Apr 2024
    Posts
    10
    Location
    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.

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    In a Worksheet Module
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  9. #9
    VBAX Regular
    Joined
    Apr 2024
    Posts
    10
    Location
    I cant find "Worksheet Module". FYI - I'm using this in a Word document.

  10. #10
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    My apologies. Document Module.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  11. #11
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Can you post your sample document. There is nothing wrong with your code. What is calling your code?
    Greg

    Visit my website: http://gregmaxey.com

  12. #12
    VBAX Regular
    Joined
    Apr 2024
    Posts
    10
    Location
    How do I post my document?
    Thanks!

  13. #13
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    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.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

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

    Sample file to test visibility and change font color

    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!
    Attached Files Attached Files

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

    Visit my website: http://gregmaxey.com

  16. #16
    VBAX Regular
    Joined
    Apr 2024
    Posts
    10
    Location
    Solved
    Fantastic!!! I like the "UpdateTextBoxVisibilityIII" the best!
    Thanks for all that helped. I continue to learn and having fun!
    Cheers!

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

    Visit my website: http://gregmaxey.com

  18. #18
    VBAX Regular
    Joined
    Apr 2024
    Posts
    10
    Location
    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!

Posting Permissions

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