Consulting

Results 1 to 4 of 4

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

Posting Permissions

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