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