Consulting

Results 1 to 4 of 4

Thread: Word - show/hide bookmark text based on checkbox

  1. #1
    VBAX Newbie
    Joined
    Apr 2024
    Posts
    2
    Location

    Question Word - show/hide bookmark text based on checkbox

    Hi - apologies if I'm misunderstanding as I'm new to this but I am just wanting to show or hide bookmark text in a Word document based on a checkbox being ticked on or off.

    I have tried:
    Private Sub Document_Open()    ' This subroutine runs when the Word document is opened
        ' It ensures that the text linked to checkboxes is hidden by default
        HideTextBasedOnCheckbox
    End Sub
    
    
    Private Sub CheckBox1_Click()
        ' This subroutine is linked to the checkbox's click event
        HideTextBasedOnCheckbox
    End Sub
    
    
    Private Sub HideTextBasedOnCheckbox()
        ' This subroutine checks the state of the checkbox and shows/hides text accordingly
        Dim cb As CheckBox
        Set cb = ActiveDocument.FormFields("CheckBox1").CheckBox
        
        If cb.Value = True Then
            ' If the checkbox is ticked, show the text
            ActiveDocument.Bookmarks("TextToDisplay").Range.Font.Hidden = False
            MsgBox "The checkbox is ticked!", vbInformation
        Else
            ' If the checkbox is not ticked, hide the text
            ActiveDocument.Bookmarks("TextToDisplay").Range.Font.Hidden = True
        End If
    End Sub
    Is there something wrong with the above code?

    Thanks
    T
    Attached Files Attached Files

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    It looks to me like you are mixing the use of Content Controls and ActiveX objects. The sub 'CheckBox1_Click' refers to an ActiveX object, however, 'ActiveDocument.FormFields("CheckBox1").CheckBox' does not refer to an ActiveX control.

    I have updated the code below and added an ActiveX checkox on the attached file.

    Private Sub Document_Open()    ' This subroutine runs when the Word document is opened
        ' It ensures that the text linked to checkboxes is hidden by default
        HideTextBasedOnCheckbox
    End Sub
    
    
    Private Sub CheckBox1_Click()
        ' This subroutine is linked to the checkbox's click event
        HideTextBasedOnCheckbox
    End Sub
    
    
    Private Sub HideTextBasedOnCheckbox()
        ' This subroutine checks the state of the checkbox and shows/hides text accordingly
        Dim cb As Object
        Set cb = CheckBox1
        
        If cb = True Then
            ' If the checkbox is ticked, show the text
            ActiveDocument.Bookmarks("TextToDisplay").Range.Font.Hidden = False
        Else
            ' If the checkbox is not ticked, hide the text
            ActiveDocument.Bookmarks("TextToDisplay").Range.Font.Hidden = True
        End If
    End Sub
    Attached Files Attached Files
    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

  3. #3
    VBAX Newbie
    Joined
    Apr 2024
    Posts
    2
    Location
    Thank you!!

  4. #4
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    You are welcome and also, welcome to the forum.
    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

Posting Permissions

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