locked
Hiding Checkbox based on Checkbox RRS feed

  • General discussion

  • Have a Word Document with multiple checkboxes.  If a box is checked, I want to hide the checkbox and text field next to it.  New to VBA code, and I must use Word.

    Greatly appreciate the assistance.

    Friday, September 21, 2012 4:53 PM

All replies

  • What sort of check box? If you hide the check box when it is checked, how will users uncheck it again should they change their minds?

    Graham Mayor - Word MVP
    www.gmayor.com

    Sunday, September 23, 2012 11:25 AM
  • Graham, I have two checkboxes. 

    One checkbox has a textfield beside it that says "External"

    One checkbox has a textfield beside it that says "Internal"

    If checkbox beside External is checked and "X" is in it, I want to hide the second checkbox and the text field beside it that says "Internal".

    Can do in a PDF, but I must use Word.

    Dave

    Sunday, September 23, 2012 3:59 PM
  • What sort of check boxes have you inserted?

    Graham Mayor - Word MVP
    www.gmayor.com

    Monday, September 24, 2012 5:03 AM
  • Under Developer, Under Controls, Under Legacy Tools, I have added two checkbox form fields.

    One checkbox is beside a text field labeled internal.

    One checkbox is beside a text field labled external.

    When the checkbox beside internal is checked (filled with an "X"), I would like the checkbox and the text field labeled external to disappear/be hidden/disabled (whatever is doable).

    I have discovered how to make the text field disappear, but not the checkbox.

    Monday, September 24, 2012 12:33 PM
  • At this point I am open to any suggestions that when I check one checkbox, another checkbox and textfield beside it disappears/is hidden or is disabled.


    I can send you the word example I have if it would help you understand what I am saying.
    • Edited by jaketsuar Monday, September 24, 2012 12:39 PM
    Monday, September 24, 2012 12:34 PM
  • I understood what you were saying - but the approach could depend on the type of check box field used. In this case create a character style based on the underlying style and call it 'Hidden'. Apply that style to the parts you want to hide. Run the following macro on exit from the first check box.

    The macro assumes the first check box is called Check1 and the second Check2 and the text field you want to hide is called Text2. Change the names in the macro as appropriate. When the macro is run by tabbing out of the check box field the font in the Hidden style is changed to have the hidden attribute and the two fields are disabled.

    Sub HideFields()
    Dim oStyle As Style
    If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
        For Each oStyle In ActiveDocument.Styles
            If oStyle.NameLocal = "Hidden" Then
                oStyle.Font.Hidden = True
                ActiveDocument.FormFields("Check2").Enabled = False
                ActiveDocument.FormFields("Text2").Enabled = False
                Exit For
            End If
        Next oStyle
    Else
        For Each oStyle In ActiveDocument.Styles
            If oStyle.NameLocal = "Hidden" Then
                oStyle.Font.Hidden = False
                ActiveDocument.FormFields("Check2").Enabled = True
                ActiveDocument.FormFields("Text2").Enabled = True
                Exit For
            End If
        Next oStyle
    End If
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    Monday, September 24, 2012 2:44 PM
  • Thank you for the coding above, I will certainly give it a shot.  I will let you know how it works when I get a chance to do it.

    David

    Monday, September 24, 2012 3:47 PM
  • Graham,

    Thank you again for the coding.  I tried to put this into a macro in Word and it would not work for me.

    The code I have performs the following:  once I click the checkbox, a text field becomes hidden automatically.  I uncheck the box, the text re-appears.  What I was trying to do was have the second checkbox disappear/be hidden as well as.

    I think I am going to place this in the "too hard/impossible" box and move on.  I appreciate your time though.

    David

    Tuesday, September 25, 2012 12:58 PM
  • If the named check box and the named text field are formatted with the same 'Hidden' character style then that's exactly what the macro does. it toggles the visibility of Check2 and Text2 according to the value of Check1. However it does this when you tab out of the field and not when you check it.

    Graham Mayor - Word MVP
    www.gmayor.com


    Tuesday, September 25, 2012 2:44 PM
  • I used a different approach... I set the text to be hidden as bookmarks, called Text1, Text2 and Text3. In the next step, I created 3 CheckBoxes and handle their events in a single procedure.

    Put the code below in the ThisDocument class:

    Private Sub CheckBox1_Click()
        ClickEvent CheckBox1
    End Sub
    
    Private Sub CheckBox2_Click()
        ClickEvent CheckBox2
    End Sub
    
    Private Sub CheckBox3_Click()
        ClickEvent CheckBox3
    End Sub
    
    Private Sub ClickEvent(ctrl As msforms.CheckBox)
        Dim sBookmarkName As String
        
        With ThisDocument
            Select Case ctrl.Name
                Case "CheckBox1": sBookmarkName = "Text1"
                Case "CheckBox2": sBookmarkName = "Text2"
                Case "CheckBox3": sBookmarkName = "Text3"
            End Select
            
            .Bookmarks(sBookmarkName).Range.Font.Hidden = _
              Not ctrl.Value
        End With
    End Sub

    If you can't reproduce my example, try download this example document: https://skydrive.live.com/redir?resid=FB206A2D510E0661!597


    Felipe Costa Gualberto - http://www.ambienteoffice.com.br

    Tuesday, October 2, 2012 10:12 PM
  • Figured it out, really simple for what I needed. 

    Sub CheckBox15_Click()

    If CheckBox15.Value = True Then

    CheckBox14.Value = False

    Else

    CheckBox14.Value = True

    End If

    End Sub

    End of story.

    Dave

    Thursday, October 4, 2012 4:47 PM
  • I didn't pay attention to the fact that you wanted to hide a text FIELD and a checkbox! Sorry. Glad you found a solution.

    P.S.: your solution doesn't hide the text field.


    Felipe Costa Gualberto - http://www.ambienteoffice.com.br

    Thursday, October 4, 2012 11:06 PM
  • I didn't provide that code, I thought if a novice such as me could figure it out that others could probably do the same. 

    While these threads are very thought provoking, I have yet to readily find solutions to the most simple of questions (again, as a novice in VB).

    Perhaps there should be a Novice VB thread room where the novices can help each other without the pro's getting involved.

    Friday, October 5, 2012 1:58 PM