There may come a time when you need to collapse sections of a word doc based on a checkbox. The sample form I'm using looks like this:
To create this form:
1. Create a new word doc and add a checkbox.
2. Type some text under the text box.
3. Select the area you wish to hide and add a bookmark: Insert->Bookmark. The Bookmark dialog will appear. Type "Section 1" as bookmark name and close.
4. Repeat the process two more times.
What we've done is create "Enclosing Bookmarks" around some text. There are actually two types of bookmarks. These are:
1) Placeholder Bookmarks which look like a beam.
2) Enclosing Bookmarks that will show up as graphics.
You can see these book marks by going to Tools->Options->Display Bookmarks.
(Bookmark indicators in Red)
Now for hooking up the checkboxes.
The Checkboxes need to call a function. So, first we have to write the function. The code should look like so:
Public Sub Hideit()
'unprotect the document
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
ActiveDocument.ActiveWindow.View.ShowHiddenText = False
'Get the range of the bookmark.
Dim rng As RangeSet rng = ActiveDocument.Bookmarks("Section1").Range
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
rng.Font.Hidden = 0
Else
rng.Font.Hidden = 1
End If
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
Now in the Checkbox, select this function and your
done! Additional Info here: http://word.mvps.org/faqs/MacrosVBA/WorkWithBookmarks.htm
Happy Coding!
Jas
Wow, I didn't even know you could do that in Word! Pretty neat. Thanks for sharing.
ReplyDelete