How to Show or Hide Word Sections with Checkbox Controls Using VBA
Question details
The user needs a method to automatically show or hide specific sections of a Word document form when a user checks or unchecks checkbox content controls.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Creating an interactive DOCM form where document sections conditionally appear or disappear based on user selections in multiple checkboxes.
- Observed behavior
- The sections need to be dynamically hidden or shown by leveraging VBA macros that detect the state of checkbox content controls and manage complex dependencies.
Ensure your document is saved as a Macro-Enabled Document (.docm) and that the Developer tab is enabled in your Word ribbon to access Content Controls and the VBA editor.
Use VBA Macro with Checkbox Content Controls and Bookmarks
This solution uses the Document_ContentControlOnExit event to toggle the hidden font property of bookmarked sections based on checkbox states.
You can link a checkbox content control to a specific section by storing the section's bookmark name in the checkbox's Tag property. The VBA macro reads this tag and hides or unhides the text associated with that bookmark whenever the user clicks outside the checkbox.
Highlight the text section you want to hide or show. Go to the Insert tab, click Bookmark, type a name with no spaces (e.g., 'Section1'), and click Add.
Go to the Developer tab, insert a Checkbox Content Control, and click Properties. In the 'Tag' field, enter the exact bookmark name you created (e.g., 'Section1').
Press Alt + F11 to open the Visual Basic for Applications (VBA) Editor. In the Project Explorer pane on the left, double-click 'ThisDocument' under your current document's project.
Paste the following macro code into the module window: Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean) Dim BookmarkName As String If CC.Type <> wdContentControlCheckBox Then Exit Sub BookmarkName = CC.Tag If ActiveDocument.Bookmarks.Exists(BookmarkName) Then ActiveDocument.Bookmarks(BookmarkName).Range.Font.Hidden = Not CC.Checked Else MsgBox "Bookmark '" & BookmarkName & "' was not found in this document." End If End Sub
Save the document. Check the box and then click anywhere outside of it. The macro runs upon exiting the control, automatically hiding or revealing the bookmarked text based on the checkbox state.

Try WPS Office for Seamless Document Management
If you frequently work with complex forms, VBA macros, or heavily formatted documents, WPS Office offers a powerful, lightweight, and highly compatible suite for all your document needs.

Frequently Asked Questions
Why isn't my section hiding immediately after I check the box?
The VBA macro relies on the `Document_ContentControlOnExit` event. This means the code will only execute when you click outside the checkbox (exiting the control) after checking or unchecking it.
Can I use Legacy Form Fields instead of Content Controls?
Yes, but legacy form fields require the document to be locked for 'Filling in forms'. They trigger macros via 'On Entry' and 'On Exit' properties in the field settings, which requires a slightly different VBA approach.
How do I make hidden text visible while editing the document?
To view hidden text while setting up your document, click the Show/Hide ¶ button on the Home tab. Alternatively, go to File > Options > Display, and check the 'Hidden text' box to always show it on the screen.




