|
A frequent question in Microsoft Word user
forums is, "How can I create a document
so that the display of certain information can be expanded or collapsed.
Applications for this type document might include a schedule of events
with the display of event details hidden unless a user elects to see the
details. Procedures might have general steps with an option to
display more detailed instructions. Teachers might prepare a set
of math problems with the option to display how the problem is worked
out, or just a simple set of question with an option to display the
answers.
This page provides a couple of methods for developing a document of
that type. We will use a simple example of questions with a toggle
option to display the answers.
The first method involves a macrobutton field
to provide user interface, a set of macros
which function as toggles, a document variable to store the toggle
value, an IF field for evaluating the toggle value and setting the
display, and an AutoText entry for displaying the
data. Sounds a bit complex, but it isn't too bad.
Here is a sample with the answers toggled off. |
|
 |
|
Here the user has toggled the display for the answers. |
|
 |
|
Before getting to the macros, lets look at the field codes used in the
document itself. |
|
a. The MACROBUTTON fields generate the display of the
radio button and fires the designated macro. The construction of
the macrobutton field is very simple. The field fires the macro
named ShowHide2 and displays the radio button symbol. 
Note: The
symbol is inserted during construction of the field with Insert>Symbol.
Use any symbol you like. |
|
b. The AUTOTEXT fields store the AUTOTEXT entry to display.
The AUTOTEXT fields are nested in the conditional IF fields and
displayed as the "True" condition.

Bonus Tip: To create an AUTOTEXT entry just type out and
select the desired text, press Insert>AutoText>AutoText. In the
"look in" window select the appropriate location (e.g., your template)
and click add |
| c. The DOCVARIABLE field (see above),
stores a value set by the toggle macros. |
| d. The IF field compares the
DOCVARIABLE value to the text "Show." IF the condition is met, the
AUTOTEXT result is displayed. IF the condition is not met then
nothing is displayed.
Bonus Tip:
For more on conditional IF fields see
Conditional Headers and
Footers. |
e. Here is the sample document with the
field codes displayed. Field code display is toggled with ALT+F9.
I have applied 4 pt font to the macrobutton fields to keep the row
height in check |
|
 |
|
Ok now for the macros. The following macros should be stored in
your a template project module. Fellow MVP Graham Mayor provides
instructions for applying macros:
Guide for
Installing Macros
Option Explicit
Const pValue_1 = "Show"
Const pValue_2 = "Hide"
Sub CallShowHide()
ShowHide Selection.Bookmarks(1).Name
End Sub
Sub ShowHide(pVarName As String)
Dim pValue As String
On Error Resume Next
pValue = ActiveDocument.Variables(pVarName)
On Error GoTo 0
If pValue = "" Or pValue = pValue_2 Then
pValue = pValue_1
Else
pValue = pValue_2
End If |
|
The second method uses "Togglebutton" control and
the "heightrule" method of a table row. |
|

|
|

|
I am attaching a template that contains all of the fields and macros for
both of the methods shown above:
Template |
|
There is usually several ways to perform a specialized task like
this in Word. Fellow MVP Klaus Linke has develelop a nifty process
using macrobuttons, VBA, and styles to expand and collapse text.
By his own admission, the are a few barbs in the code. A copy of
Klaus's work is provided here:
Small Demo of MacroButton
Fields |
|
|
Looking for something else?
|
|
|
|
|