Word

Word: Toggle the visibility of a table based on a checkbox value

Ease of Use

Intermediate

Version tested with

2002 

Submitted by:

fumei

Description:

Uses the checked value of a checkbox to show, or hide, a table. 

Discussion:

While this KB entry uses a table, in fact, any portion of a document could be hidden, or displayed, based on the checked value of a checkbox. It does not even have to be a checkbox. It could be based on a user text input, or a series of dropdown choices. Essentially you are taking a logic value and hiding something based on that. In this case, it is a table. The table is selected, and checked to see if it is already hidden. If it is and the logic choice is NOT to be hidden, then the routine unhides it. Using a checkbox makes it either ON (checked - table is hidden), or OFF (unchecked - table is showing). But you make the logic the other way around if you like. The code belows assumes there is an ActiveX checkbox (named Checkbox1), with a table after it. It does not matter how much text is between the checkbox and the table. The code actions against the NEXT table after the checkbox. In the attached file, there is also a second checkbox. This one toggles the visibility of a block of text. It makes a Range of the bookmarked text and toggles the font visibility. This is demonstrate that ANY part of a document, not just specifically a table, can be toggled visibile, or not. 

Code:

instructions for use

			

Option Explicit Sub CheckBox1_Change() Call ShowHideTable End Sub Sub ShowHideTable() With Selection .GoTo What:=wdGoToTable, Which:=wdGoToNext, _ Count:=1, Name:="" .Tables(1).Select End With If CheckBox1.Value = True Then With Selection.Font .Hidden = True End With With ActiveWindow.View .ShowHiddenText = False .ShowAll = False End With Else With Selection.Font .Hidden = False End With With ActiveWindow.View .ShowHiddenText = True .ShowAll = True End With With Selection .Collapse direction:=wdCollapseStart .MoveLeft unit:=wdCharacter, Count:=1 End With End If End Sub ' the other View properties ' if you want to still see paragraph marks, you ' must explicitly turn it on = True ' .ShowAnimation = True ' .Draft = False ' .WrapToWindow = False ' .ShowPicturePlaceHolders = False ' .ShowFieldCodes = False ' .ShowBookmarks = False ' .FieldShading = wdFieldShadingWhenSelected ' .ShowTabs = False ' .ShowSpaces = False ' .ShowParagraphs = False ' .ShowHyphens = False ' .ShowHiddenText = False ' .ShowAll = True ' .ShowDrawings = True ' .ShowObjectAnchors = False ' .ShowTextBoundaries = False ' .ShowHighlight = True ' .DisplayPageBoundaries = True ' .DisplaySmartTags = True ' this is to do the same thing with a bookmark ("mytext") ' using a second checkbox (Checkbox2) Sub CheckBox2_Change() Call ShowHideBookmark End Sub Sub ShowHideBookmark() Dim orange As Range Set orange = ActiveDocument.Bookmarks("mytext").Range If CheckBox2.Value = True Then With orange.Font .Hidden = True End With With ActiveWindow.View .ShowHiddenText = False .ShowAll = False End With Else With orange.Font .Hidden = False End With With ActiveWindow.View .ShowHiddenText = True .ShowAll = True End With End If End Sub

How to use:

  1. Create a document with an ActiveX checkbox, followed by a table. The ActiveX checkbox is inserted using the Controls toolbar. View > Toolbars > Controls.
  2. OR
  3. unzip the attached file, and open it to see how it works.
  4. Open the VBE (Alt-F11)
  5. Copy the above code in the ThisDocument module of the document. It MUST be in the ThisDocument module. It can be elsewhere, but the code would need to be changed a bit.
  6. Switch back to the document, and save the file
 

Test the code:

  1. Click the checkbox. If the checkbox is CHECKED the table is hidden, if it is UNCHECKED the table is visible.
 

Sample File:

ShowHideTable.zip 14.18KB 

Approved by mdmackillop


This entry has been viewed 166 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express