
The Anchorage
Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
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 of document might include:
This Microsoft Word Help & Microsoft Word Tips page provides some methods and techniques to allow the document user to show/hide (or expand/collapse) document content.
The first method uses a combination of fields, a bookmark, VBA procedures, a document variable, and AutoText.
It sounds complex, but it really isn't. The following illustrates sample text using this method showing the data collapsed and expanded.


First lets look and the fields. The following depicts the document with field codes displayed.
Note: The second illustration shows the field code of one of the two macrobutton fields expanded horizontally for clarity. I've applied 4 pt. font size to the actual macrobutton fields to keep the row height in check when displaying the field code.



Note: For more on conditional IF fields see my: Conditional Headers and Footers.
VBA procedures shown in the code pane below are used to set a value in the in a document variable. I have placed bookmarks around the MacroButton fields to create a named range. This simplifies the VBA procedures.

Option Explicit Const strValue_1 = "Show" Const strValue_2 = "Hide" Sub CallShowHide() 'This is the procedure defined in the MACROBUTTON. 'It is the current selection and it is bound by a bookmark. 'Call procedure and pass bookmark name as argument. ShowHide Selection.Bookmarks(1).Name lbl_Exit: Exit Sub End Sub Sub ShowHide(ByRef strVarName As String) Dim strValue As String 'An error will occur if the document variable does not yet exist. On Error Resume Next 'What is the value of the named document variable if it exists? strValue = ActiveDocument.Variables(strVarName) On Error GoTo 0 'Set\toggle value If strValue = "" Or strValue = strValue_2 Then strValue = strValue_1 Else strValue = strValue_2 End If 'Create/Redefine document variable value. ActiveDocument.Variables(strVarName).Value = strValue 'Let the IF field do its deed. ActiveDocument.Fields.Update lbl_Exit: Exit Sub End Sub
See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.
A table row can serve as an ideal defined range which can be expanded and collapsed to show or hide document data.


ActiveX controls carry some baggage (see: ActiveX Form Controls ), but they work well in this application. Since ActiveX controls are properties of the special ThisDocument class place the following code in the "ThisDocument" module of the VB project.
Private Sub ToggleButton1_Click()
With ActiveDocument.Tables(2).Rows(2)
If .HeightRule = wdRowHeightExactly Then
.HeightRule = wdRowHeightAuto
ToggleButton1.Caption = "Hide"
.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
Else
ToggleButton1.Caption = "Show"
.HeightRule = wdRowHeightExactly
.Height = ".5"
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
End If
End With
lbl_Exit:
Exit Sub
End Sub
There is usually several ways to perform a specialized task like this in Word. Word MVP Klaus Linke has develop a nifty process using MacroButton fields, VBA, and styles to expand and collapse text. By his own admission, there are a few barbs in the code. A copy of Klaus's work is provided in this tips page demonstration package.
Note: This tips page, illustrations and examples were developed using Word 2003. It is wholly functional with Word 2007 and 2010.
That's it! I hope you have found this tips page useful and informative. I'm including a demonstration package containing the documents used to create this tips page here: Toggle Data Display.
Do you want to make a payment for consulting work or donate to help support this site?
PayPal is a safe, easy way to pay online.
Use the appropriate currency "Donate" button to make a payment or donation.