logo
search
VBA & Macro Problems

How to Show or Hide Word Sections with Checkbox Controls Using VBA

Chanuka GeekiyanageChanuka Geekiyanage Sep 28, 2026 869 views

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.

How to Show or Hide Word Sections with Checkbox 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.
Before you start

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.

Solution 1Recommended

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.

1
Bookmark the target section

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.

2
Insert and configure the Checkbox Content Control

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').

3
Open the VBA Editor

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.

4
Add the VBA code

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

5
Test the macro

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.

Use VBA Macro with Checkbox Content Controls and Bookmarks
Advanced Logic for Multiple Dependencies: If multiple checkboxes control a single section or have overlapping dependencies, replace the basic Tag-to-Bookmark mapping with 'Select Case' statements in the macro to evaluate specific combinations of checkbox values.
Free Microsoft Office alternative

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.

Excellent compatibility with Microsoft Word formats (.docx, .doc, .docm)Lightweight installation that runs smoothly on Windows, Mac, and LinuxFamiliar user interface requiring zero learning curveFeature-rich free version available for everyday document tasks
microsoft office alternative - wps office

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.