Microsoft Word - Show/hide text based on multiple checkbox

Asked By Cleo on 09-Aug-10 09:56 AM
I have a document set up to either hide or display text based on whether a single box is checked, for example:

Private Sub Assoc_below_Click()
    Select Case Assoc_below.Value
      Case True
        ActiveDocument.Bookmarks("Assoc_below").Range.Font.Hidden = False
      Case False
        ActiveDocument.Bookmarks("Assoc_below").Range.Font.Hidden = True
    End Select
End Sub

I need to set this up so that text will hide or display only if MULTIPLE boxes are checked.  This should be pretty simple not sure how to integrate it.

Can anyone please tell me how to write this code?  Thanks!
Ken Fitzpatrick replied to Cleo on 09-Aug-10 11:21 AM
Cleo,

What you could do is create a handler for each checkbox (just like you did with the one). In each handler, start by hiding the text. Then check the value of the checkbox. If it is true, use an if statement and check the Checked property of the other. If that is also true, then show your text. For example, this psuedocode illustrates what I am talking about (except create a handler for both):
Private Sub Assoc_below_Click()
  ActiveDocument.Bookmarks("Assoc_below").Range.Font.Hidden = True
  Select Case Assoc_below.Value
    Case True
    if <other checkbox>.checked then
       ActiveDocument.Bookmarks("Assoc_below").Range.Font.Hidden = False
    end if
  End Select
End Sub

Hope that is clear,

Ken
Cleo replied to Ken Fitzpatrick on 09-Aug-10 12:34 PM
Thanks for the reply Ken.  I created the handler text for both checkboxes. However, I'm receiving the following error for line "If Checkbox2.Checked Then"

Object doesn't support this property or method.

Thanks for the help.
Venkat K replied to Cleo on 09-Aug-10 12:41 PM
Try this:
Sub DupCheckMark()
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
ActiveDocument.FormFields("Check2").CheckBox.Value = True
ActiveDocument.FormFields("Check3").CheckBox.Value = True
Else
ActiveDocument.FormFields("Check2").CheckBox.Value = False
ActiveDocument.FormFields("Check3").CheckBox.Value = False
End If
End Sub

Thanks
Cleo replied to Venkat K on 09-Aug-10 04:17 PM
The code below works for displaying text when two checkboxes are checked.  Does anyone know how to edit the code to display text only when THREE checkboxes are checked?

Private Sub Checkbox1_Click()
  ActiveDocument.Bookmarks("Text").Range.Font.Hidden = True
  Select Case Checkbox1.Value
    Case True
    If Checkbox2.Value = True Then
     ActiveDocument.Bookmarks("Text").Range.Font.Hidden = False
    End If
  End Select
End Sub

Private Sub Checkbox2_Click()
  ActiveDocument.Bookmarks("Assoc_below").Range.Font.Hidden = True
  Select Case Checkbox2.Value
    Case True
    If Checkbox1.Value = True Then
     ActiveDocument.Bookmarks("Text").Range.Font.Hidden = False
    End If
  End Select
End Sub

Thanks in advance!
Ken Fitzpatrick replied to Ken Fitzpatrick on 09-Aug-10 07:11 PM
Sorry, didn't realize there wasn't a checked property. Looks like the Value property works though.
Ken Fitzpatrick replied to Cleo on 09-Aug-10 07:18 PM
Cleo,

How many checkboxes can you have? Are there only three and all three have to be checked to display the text, or could there be more than three? For example, if there are 9 checkboxes, you want to display the text if 3 out of the 9 are checked?

Please clarify,

Ken
Cleo replied to Ken Fitzpatrick on 10-Aug-10 02:05 PM
Hi Ken,

The template I am creating has around 20 checkboxes right now.  Users can check off however many, depending on the criteria they need to use for their document.  For one section, I want certain text to appear if 3 specific checkboxes are selected, but not limited to the 3, (e.g., text will appear when check box 1, 2, 3 and 5, 6 are selected; text will hide when only 1, 2 and 5, 6 are selected).  Please let me know if that isn't clear.

Thanks!
Cleo
Ken Fitzpatrick replied to Cleo on 10-Aug-10 02:23 PM
Cleo,

So basically, for the text to show up, checkboxes 1, 2 and 3 have to be selected? Correct?
If so, here is how you can do that:
Private Sub Checkbox1_Click()
   ActiveDocument.Bookmarks("Text").Range.Font.Hidden = Not AllChecked()
End Sub
  
Private Sub Checkbox2_Click()
   ActiveDocument.Bookmarks("Text").Range.Font.Hidden = Not AllChecked()
End Sub
  
Private Sub Checkbox3_Click()
   ActiveDocument.Bookmarks("Text").Range.Font.Hidden = Not AllChecked()
End Sub
  
Private Function AllChecked() as Boolean
   Return Checkbox1.Checked And CheckBox2.Checked Abd Checkbox3.Checked
End Function
Here is how it works: We don't really care about any checkbox except 1, 2, and 3 (If I understood you). If that is the case, then create a function called AllChecked() that returns True is they are all checked, else False if they are not. The simplest way to do that is to return the boolean value of "And'ing" all of the checkboxes checked state (See AllChecked() function in my code sample above). If one or more of them is not checked, then the return value will be false. The only way it will return True is if they are all true (e.g. True And False And True = False; True And True And True = True; False And True And False = False). Does that make sense?
Next, in each click handler, we can set the Hidden property to the opposite of the returned Boolean value of AllChecked. for example, if AllChecked = True, then Not AllChecked = False, and vice-versa.
 
If you add more checkboxes to the criteria, then add them to the AllChecked() function. You can also support more complicated logic by changing how the AllChecked() function works. As long as it returns True or False and does it correctly, you can easily hide or show your text.

Hope that helps and makes sense.

Ken
Cleo replied to Ken Fitzpatrick on 10-Aug-10 02:46 PM
Ken,

Correct - that is what I'm looking for.  I've tried this latest script, and it's giving me an "Expected: end of statement" error for the highligted word in the following line:

Return Checkbox1.Checked And CheckBox2.Checked And Checkbox3.Checked

Thanks!


Ken Fitzpatrick replied to Cleo on 10-Aug-10 03:31 PM
Sorry about that Cleo, I have been doing VB.Net development for so long, I forgot how to return values from traditional VB functions. Try this instead:
Private Function AllChecked() As Boolean
   AllChecked = CheckBox1.Value And CheckBox2.Value And CheckBox3.Value
End Function

This time I tested it in a Word document and it works fine.

For anyone else reading this, this is the full solution for Tradiation VB:
Private Sub Checkbox1_Click() 
   ActiveDocument.Bookmarks("Text").Range.Font.Hidden = Not AllChecked() 
End Sub 
    
Private Sub Checkbox2_Click() 
   ActiveDocument.Bookmarks("Text").Range.Font.Hidden = Not AllChecked() 
End Sub 
    
Private Sub Checkbox3_Click() 
   ActiveDocument.Bookmarks("Text").Range.Font.Hidden = Not AllChecked() 
End Sub 
    
Private Function AllChecked() as Boolean 
   AllChecked = Checkbox1.Checked And CheckBox2.Checked And Checkbox3.Checked 
End Function

You can use the same technique for any type of situation that requires a boolean value in VB.Net, C#, Java, etc...

Ken


Cleo replied to Ken Fitzpatrick on 10-Aug-10 03:35 PM
Ken - yes, this works now.  Thanks very much for staying with me through this!
Ken Fitzpatrick replied to Cleo on 10-Aug-10 03:37 PM
Awesome, no problem!