Share via

Checkboxes Issue

Anonymous
2024-03-24T13:57:30+00:00

I have three checkbox content controls. One of them turns the font color of the text in a text content control white, the other two turn it black. This is working properly using Document_ContentControlOnEnter. However, when the user clicks on the text content control (which has editing locked set after the change in font color), either of the two checkboxes that turn the font black uncheck themselves and the text font turns white. Why is this happening?

Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)

On Error Resume Next

Dim exportControl As ContentControl

' Check if "export" control exists

If ActiveDocument.SelectContentControlsByTitle("export").Count > 0 Then

Set exportControl = ActiveDocument.SelectContentControlsByTitle("export").Item(1)

' Unlock Content Control

exportControl.LockContents = False

End If

If ContentControl.Title = ("Check1") And ContentControl.Checked = True Then ActiveDocument.SelectContentControlsByTitle("Check2").Item(1).Checked = False ActiveDocument.SelectContentControlsByTitle("Check3").Item(1).Checked = False ' Sets font to white ActiveDocument.SelectContentControlsByTitle("export").Item(1).Range.Font.Color = RGB(255, 255, 255) ' Lock Content Control exportControl.LockContents = True

ActiveDocument.SelectContentControlsByTitle("emptxt1").Item (1)

ElseIf ContentControl.Title = ("Check2") And ContentControl.Checked = True Then ActiveDocument.SelectContentControlsByTitle("Check1").Item(1).Checked = False ActiveDocument.SelectContentControlsByTitle("Check3").Item(1).Checked = False ' Sets font to black ActiveDocument.SelectContentControlsByTitle("export").Item(1).Range.Font.Color = RGB(0, 0, 0) ' Lock Content Control exportControl.LockContents = True

ActiveDocument.SelectContentControlsByTitle("emptxt1").Item (1)

ElseIf ContentControl.Title = ("Check3") And ContentControl.Checked = True Then ActiveDocument.SelectContentControlsByTitle("Check1").Item(1).Checked = False ActiveDocument.SelectContentControlsByTitle("Check2").Item(1).Checked = False ' Sets font to black ActiveDocument.SelectContentControlsByTitle("export").Item(1).Range.Font.Color = RGB(0, 0, 0) ' Lock Content Control exportControl.LockContents = True

ActiveDocument.SelectContentControlsByTitle("emptxt1").Item (1)

End If

' Lock Content

Control exportControl.LockContents = True

End Sub

Microsoft 365 and Office | Word | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. John Korchok 232.8K Reputation points Volunteer Moderator
    2024-03-24T17:18:21+00:00

    To debug VBA code, comment out the On Error Resume Next line so you can see where the errors in your code are.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2024-03-25T13:25:25+00:00

    Thank you Greg, that did it.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-03-25T13:03:02+00:00

    Because you entered a text content control and your code is asking If 1) Is the Check 1, 2 or 3 "AND" 2) is it checked. "Checked" can't be evaluated for a text control. You will have to do something like:

    If Title = "Check1" Then
    
       If Checked then
    

    BREAK,

    Not sure why you have two checkboxes to toggle the text back to displayed? Also, what should the state be if none of the boxes are checked?

    If you just want to use a single checkbox to toggle the state of "export" text and that test is some fixed value, you could use XML Mapping and the Document_ContentControlBeforeContentUpdate Event. The advantage is there is no requirement to physically enter or exit the control. (Other than enter initially to check or uncheck)

    Private Sub Document_ContentControlBeforeContentUpdate(ByVal ContentControl As ContentControl, Content As String)
    Dim oXMLPart As CustomXMLPart
    Dim strExportText As String
    strExportText = "This item is restricted from export."
    Select Case ContentControl.Title
    Case Is = "Check1"
    Set oXMLPart = ContentControl.XMLMapping.CustomXMLPart
    If Content = True Then
    oXMLPart.SelectSingleNode("/ns0:CC_Map_Root[1]/ns0:export[1]").Text = strExportText
    Else
    oXMLPart.SelectSingleNode("/ns0:CC_Map_Root[1]/ns0:export[1]").Text = " "
    End If
    End Select
    lbl_Exit:
    Exit Sub
    End Sub

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2024-03-25T11:17:53+00:00

    Hi John,

    Okay, commented that line out. Checked the box (N) to turn font white. Then checked box (Part 810) to turn font black. Then clicked on the text content control and get this error. When I click on Debug, the line highlighted for the error is for Check1 checkbox, for which the property is valid.

    Was this answer helpful?

    0 comments No comments