Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Toggle Data Display
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

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.

Method 1 – Fields and AutoText

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.

toggle data display 1
Data collapsed
toggle data display 2
Data expanded

First lets look and the fields. The following depicts the document with field codes displayed.

Site Note IconNote:  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.

toggle data display 3
Actual field code text

toggle data display 4
MACROBUTTON field expanded horizontally for clarity

toggle data display 5
IF field enlarge for clarity depicting nested DOCVARIABLE & AUTOTEXT fields.

Site Note IconNote: 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. 

toggle data display 6
VBA Script:
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

Site Note icon See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.

Method 2 – Expand/Collapse Defined Range

A table row can serve as an ideal defined range which can be expanded and collapsed to show or hide document data.

toggle data display 7

toggle data display 8

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.

VBA Script:
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.

Site Note iconNote: 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.

PAYMENTS/DONATIONS

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

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.


Search my site or the web using Google Search Engine

Google Search Logo

Or

JustAnswerAsk a Word Expert OnlineSubmit