Reputation: 47
I'm in the process of trying to put together a form in ms-word, in which it is desired that if a user checks 1 of 3 check-box options, the other two and their adjacent text will be hidden.
I'm aware of how to hide the content some-what, but I'm completely unfamiliar with VBA/programming as a whole, so I don't quite know what is wrong in my If/Else loop, but it definitely doesn't show/hide everything.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
If ContentControl.Title = "checkbox1" And ContentControl.Checked = True Then
ActiveDocument.Bookmarks("Approve").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Approve").Range.Font.Hidden = False
End If
If ContentControl.Title = "checkbox2" And ContentControl.Checked = True Then
ActiveDocument.Bookmarks("Denied 1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("Denied 2").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Sign1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("Sign2").Range.Font.Hidden = False
End If
If ContentControl.Title = "checkbox3" And ContentControl.Checked = True Then
ActiveDocument.Bookmarks("pending").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("pending").Range.Font.Hidden = False
End If
End Sub
If I only keep one of the if/else parts it does work fine, but when I keep all 3 it doesn't work.
Any guidance is really appreciated!
Upvotes: 0
Views: 651
Reputation: 166241
Here:
If ContentControl.Title = "checkbox1" And ContentControl.Checked = True Then
ActiveDocument.Bookmarks("Approve").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Approve").Range.Font.Hidden = False
End If
You set "approve" to hidden if the title is "checkbox1" and Checked is True, but the Else
will run when only one (or neither) of those is true. So your Else
clauses always run in the other two blocks not connected to the clicked control.
would be better like this:
If ContentControl.Title = "checkbox1" Then
If ContentControl.Checked = True Then
ActiveDocument.Bookmarks("Approve").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Approve").Range.Font.Hidden = False
End If
End If
or (shorter):
If ContentControl.Title = "checkbox1" Then
ActiveDocument.Bookmarks("Approve").Range.Font.Hidden = _
(ContentControl.Checked = True)
End If
Whole thing:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim bChecked As Boolean
bChecked = (ContentControl.Checked = True)
If ContentControl.Title = "checkbox1" Then
ActiveDocument.Bookmarks("Approve").Range.Font.Hidden = bChecked
End If
If ContentControl.Title = "checkbox2" Then
ActiveDocument.Bookmarks("Denied 1").Range.Font.Hidden = bChecked
ActiveDocument.Bookmarks("Denied 2").Range.Font.Hidden = bChecked
End If
If ContentControl.Title = "checkbox3" Then
ActiveDocument.Bookmarks("pending").Range.Font.Hidden = bChecked
End If
End Sub
Upvotes: 1