What you need:
An ActiveX checkbox. NOT a formfield - although it can be done with formfields, it just takes a bit more code.
OK, so you have an ActiveX checkbox. Oh, just in case....this means a checkbox inserted into the document using the
Controls toolbar. Do not forget to turn off Design mode after you insert the checkbox.
A table. The code belows actions the NEXT table after the checkbox. This can easily be adjusted for any table you like.
Put the code in the ThisDocument module. It is important that the code be in the ThisDocument, as ActiveX control event are referenced directly from there. You can call control events from other nodule, but it takes a little more syntax.
Anyway, copy the code into ThisDocument. I have it set up that CHECKING the box disappears the table; UNCHECKING makes the table be visible. You can easily reverse this if you choose.
Code:
Private 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
Selection.Font.Hidden = True
ActiveWindow.View.ShowAll = False
Else
Selection.Font.Hidden = False
ActiveWindow.View.ShowAll = True
With Selection
.Collapse direction:=wdCollapseStart
.MoveLeft unit:=wdCharacter, Count:=1
End With
End If
End Sub
NOTE:
This does NOT have full error trapping!
Remember that the above actions the NEXT table.
There are a LOT of other properties associated with ActiveWindow.View. Such as:
.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
So if you want, say, the paragraph marks visible, but the table invisible, then you have to explicitly add .ShowParagraphs = True to the routine.
If you wanted to have a range of text toggled, then use a Range. I made a bookmark of about four pages of text, named it "mytext", and used a second checkbox (Checkbox2) to toggle the text visibility.
Code:
Private Sub CheckBox2_Change()
Call ShowHideBookmark
End Sub
Sub ShowHideBookmark()
Dim orange As Range
Set orange = ActiveDocument.Bookmarks("mytext").Range
If CheckBox2.Value = True Then
orange.Font.Hidden = True
ActiveWindow.View.ShowAll = False
Else
orange.Font.Hidden = False
ActiveWindow.View.ShowAll = True
End If
set oRange = Nothing
End Sub
Hope this helps.
Gerry