- Home
- Computer and Technology
- Show & Tell: Hiding Paragraphs, Pages & Sections In Word
Show & Tell: Hiding Paragraphs, Pages & Sections In Word
- By Cathy Evans
- Published 11/4/2007
- Computer and Technology
- Unrated
Cathy Evans
Work extensively with Microsoft Word, Access, Excel, building databases, userforms, mail merges and more.
View all articles by Cathy EvansShow & Tell: Hiding Paragraphs, Pages & Sections In Word
Let's say you have a letter that has several paragraphs or sections that may apply to limited audiences, but the rest of the letter applies to all recipients. For example, a letter to a contractor may advise them their bid was either accepted or rejected.
Or a letter from Human Resources to job candidates may have different statements in some areas, depending on the job specifications. Regardless of the reason, using a combination of visual basic and bookmarks is an effective way to accomplish the task.
Thanks to the help of a colleague at work who taught me how to set up this type of letter, it opened up a whole new way of approaching standard form letters. To set up the letter, determine which paragraphs or sections need to appear for each set of circumstances. Select the paragraph or section, and give it a bookmark name, something that is meaningful to you, such as 'BidAcceptance'.
Press Alt/F11 to access the visual basic editor. Insert a userform (insert/userform). Once the userform has been added, click on the form to bring up the 'toolbox', which contains command buttons, radio buttons, check boxes, text form fields and more. My preference is to also select view/properties window to have quick access to the form and field properties.
Click on the userform to bring up the toolbox, and add some radio option buttons, as many as you have paragraph/sections needing to show or hide. Name the option button by selecting it and renaming it to something meaningful. This is done by changing the name in the properties window, where you can also edit the caption and other properties, such as font, color & size. Using the toolbox, add two command buttons to the bottom of your userform, one with a caption saying 'OK' and one with a caption saying 'Cancel'.
Right click on the 'OK' button, select 'view code' and add the following (replacing Para1, etc. with your actual bookmark name, and optPara1, etc., with your actual radio option button name). The sample below assumes there are only 2 bookmarked paragraph/sections, but if you have more than 2, simply copy/paste for each IF statement as needed.
'check value of option button 1, if it is checked, find the bookmark for first paragraph or section, and select, then hide it. If value is false, it will do nothing.
If optPara1.Value = True Then
Selection.GoTo What:=wdGoToBookmark, Name:="Para1"
With Selection.Font
.Hidden = True
End With
End If
'check value of option button 2, if it is checked, find the bookmark for next paragraph or section, and select, then hide it. If value is false, it will do nothing.
If optPara2.Value = True Then
Selection.GoTo What:=wdGoToBookmark, Name:="Para2"
With Selection.Font
.Hidden = True
End With
End If
'closes the routine by unloading/closing the userform Unload Me
Then right click on your 'Cancel' button, select 'view code', and add the following.
'unloads/closes the userform
Unload Me
To force the document to save as new document and force the userform to show when the document opens, add the following to the 'This Document' section in the visual basic editor. This allows the master letter to stay intact. If you do not need to save the document each time, simply remove everything except UserformName.show (replacing userformname with your actual form name)
Private Sub Document_Open()
'opens save-as dialog box
Dim strDocName$
strDocName = ""
With Dialogs(wdDialogFileSaveAs)
' Set name in SaveAs dialog
.Name = strDocName
.Show
End With
'opens userform
UserformName.show
End Sub
The object is to have a multi-purpose form that is broad enough for a wider audience, but may have particular pieces of information that are relevant only to certain target groups. To pass these pieces of information to the right targets, a userform coupled with radio option buttons gives a nice method to be able to choose which paragraphs or sections to show or hide.
The userform might look something like this:
Please select the appropriate letter type. (Userform instructions)
__ Complete Letter
__ Partial Letter With Hidden Paragraphs
__ OK __ Cancel
This form was constructed using Word 2002 and is an example of using the hide/unhide text function. To test this ability, simply select some text in a document, from the menu select Format/Font and place a checkmark beside 'hidden'. The code used for this example was extracted by using the macro recorder. From the menu select View/Toolbars/Visual Basic. Use the 'record macro' button to repeat the steps of selecting text and formatting as hidden. You can add a bookmark and first choose to go to that bookmark (insert/bookmark, then select it and 'go to'). Then use the 'run macro' button on the visual basic toolbar to 'edit' the macro and explore the code.
You will notice when you record a macro, that there are many lines of code that are not necessarily needed. For example, this is what was originally generated:
Selection.GoTo What:=wdGoToBookmark, Name:="TestBookmark"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Font
.Name = "Verdana"
.Size = 11
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = True
.SmallCaps = False
.AllCaps = False
.Color = wdColorAutomatic
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
End With
End Sub
However, the only code that was really needed in this case was:
Selection.GoTo What:=wdGoToBookmark, Name:=" TestBookmark"
With Selection.Font
.Hidden = True
End With
To ensure the right reference libraries are in use for any visual basic project, open the visual basic editor, select tools/references, and note what is checked. If there are any are checked as MISSING, they need to be unchecked. If you already have an existing letter you would like to automate to 'show and tell', be sure to make a backup before you begin!
