Conditional statements to include or drop entire sections?

I am working on a rather lengthy template that will be used by a variety of backgrounds. As such, the intent is for the user to pick and choose which sections will apply (be left in the document). How do I tell Word that based on a yes or no (option button) to include or hide an entire section? Does the section need to be inside a single cell table?

Thank You

K

Answer
Answer

Since we'll be adding macros, start by resaving your document, changing the Save as type to Word Macro-Enabled Document (*.docm).

OK, then you want to start by selecting the sections of text that you want to turn off and on, then applying bookmarks to them. Any text that always appears does not need to be wrapped in a bookmark. If there is a complete section that is optional make sure you include the section break at the end as the last object in the bookmark.

To select a section break, you need to see it. In Word, click on the FILE tab, then on Options. Click on the Display tab and check Show all formatting marks. OK Out. Section breaks display as horizontal lines at the end of a section.

Give the bookmarks names that make relate to the content, this makes it easier to remember when programming.

When all the bookmarks are in place, it's time to program. If the Developer tab isn't visible on the Ribbon, follow these steps:

  1. In Word, click on the FILE tab, then on Options.
  2. Click on Customize Ribbon.
  3. In the right-hand list under Customize the Ribbon, look for Developer and check it. OK Out.

Now that the Developer tab shows, click on it and select Visual Basic. This displays the VBA editor screen. Follow these steps:

  1. On the left-hand side, note that there is Normal and at least one other file that is your document. Make sure your document is selected, this will ensure that your programming is stored in it. If Normal were selected, the programming would stay on your computer and not travel with the file.
  2. From the Insert menu, choose UserForm. A window opens with a blank dialog box, known in programmer speak as a user form..
  3. Press F4 to show the Properties list. This shows every detail of the dialog box.
  4. Double-click on the UserForm1 text beside the Caption heading. This is the title of your form. Enter your preferred title and it will preview in the form itself.
  5. Click on the form. The Toolbox becomes visible. This is where you get the controls that appear on the form. Float your mouse over the Toolbox until you find the CheckBox. Drag one to your form.
  6. Now that the Checkbox is highlighted, the Properties panel is no longer showing the dialog box properties. It shows the properties for the selected CheckBox1. Click on the field beside Caption and replace CheckBox1 with the name of the section to be inserted i.e. Introduction. Click on the field beside Name and replace CheckBox1 with the name of the bookmark for that section i.e. IntroSection.
  7. Insert as many checkboxes as there are sections to insert, repeating the above naming.
  8. Look in the Toolbox for the CommandButton control. Drag one to the dialog box.
  9. Change its caption to OK. This is your OK button.
  10. Double-click on the OK button. A new screen opens that shows the routine that will run when the OK button is clicked. There are opening and closing lines that read:

Private Sub CommandButton1_Click()

End Sub

  1. Click in between those two lines and paste in the following starter code:

  If ActiveDocument.Bookmarks.Exists("IntroSection") = True Then
    If IntroSection.Value = True Then  'This is the line that reads whether the CheckBox is checked or not.
      If ActiveDocument.Bookmarks("IntroSection").Range.Font.Hidden = True Then
        ActiveDocument.Bookmarks("IntroSection").Range.Font.Hidden = False
      End If
    Else
      If ActiveDocument.Bookmarks("IntroSection").Range.Font.Hidden = False Then
        ActiveDocument.Bookmarks("IntroSection").Range.Font.Hidden = True
      End If
    End If
  End If

Notice how the Bookmark name for each section appears 6 times in this block: 5 for the name of the bookmark and once for the name of the CheckBox control. Now paste this block enough times to have one block for each appearing/disappearing section. In each block, enter the Bookmark name for the corresponding section and checkbox.

Finally, paste in this line to close the dialog box. It goes right before the End Sub line:

Unload UserForm1

All of the above goes inside the CommandButton1_Click sub and runs when the OK button is clicked.

You could get away with just the above, but let's get classy and add a Cancel button:

  1. From the Toolbox, drag another CommandButton over. This one automatically gets named CommandButton2.
  2. Change the Caption to Cancel.
  3. Double-click on the button. You'll see the following:

Private Sub CommandButton2_Click()

End Sub

Edit it to this:

Private Sub CommandButton2_Click()

  Unload UserForm1

End Sub

Finally, let's make this dialog open when the Word document opens. In the VBA Editor, choose Insert>Module. Enter the following code:

Sub AutoOpen()

  Load UserForm1

  UserForm1.Show

End Sub

Save the document and test. That will get you started. It's not the most sophisticated programming style, but works and it's relatively simple for you to implement. Let us know if you run into any snags and we'll help you out.

Author of "OOXML Hacking - Unlocking Microsoft Office's Secrets", ebook now out
John Korchok, Production Manager
production@brandwares.com

11 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 3, 2024 Views 4,831 Applies to: