How do I hide sections of my document based on selection?

B

BrantRaven

Hi Everyone,

I am creating a template that will be used for requirements gathering as an
input to various high level designs. As you might image the template has
several sections that need to be completed. Across different project however,
not all sections will apply or need to be considered.

I need a way to create a summary list of all the sections at the start of
the document to allow users to check the sections they will require. Based on
this input sections in the document will then be hidden if they are not
required and the document sections will be numbered correctly in order.

Any ideas on how this may be achieved?

TIA.....

Brant
 
G

Gordon Bentley-Mix

Brant,

The answer depends on whether you really want to hide the inapplicable
sections or if it's acceptable to omit the sections entirely. Omitting the
sections is easier; hiding them a bit more difficult - especially in light of
the requirement to maintain the numbering integrity. (I'm assuming that the
numbering is applied through the use of a numbered style of some sort, and if
it's not I'd suggest that it be changed to work that way.)

If omitting the sections entirely is acceptable then there are a couple of
solutions:

You can wrap a bookmark around each section and delete the bookmark range
based on the user's input. The code would look something like this:

If CheckBox1.Value = False Then
ActiveDocument.Bookmarks("Section1").Range.Delete

Alternatively, you could start with a blank page containing a series of
bookmarks as placeholders for each section, and then use AutoText entries to
insert the appropriate content - something like this:

If CheckBox1.Value = True Then
ActiveDocument.AttachedTemplate.AutoTextEntries("Section1").Insert
ActiveDocument.Bookmarks("Section1").Range, True

If the inapplicable sections _must_ be hidden (perhaps because the users
need to be able to rerun the macro and add sections back in) then wrap a
bookmark around the sections as above and then set the .Hidden property of
the bookmark range accordingly - like so:

If CheckBox1.Value = True Then
ActiveDocument.Bookmarks("Section1").Range.Font.Hidden = False Else
ActiveDocument.Bookmarks("Section1").Range.Font.Hidden = True

However, in this case you will need to do something to the section title to
sort out the numbering - possibly by changing the style to a non-numbered
one. This can be tricky, and the macro needs to address re-applying the
original style if the macro is rerun and the section restored.

Obviously these are extremely stripped down examples, but it should give you
an idea of where to begin. You would want to add some error handling (if, for
example, a bookmark was missing or an AutoText entry couldn't be found), and
you'd probably want to avoid working with the ActiveDocument object if you
can help it. You might also want to do something about retaining / restoring
the bookmarks if you need to support rerunning the macro - possibly by
ensuring that the bookmarks are included in the AutoText entry or by adding
the bookmark back in through the use of a Range object. Alternatively, there
might be a bit of clean required to remove the extraneous bookmarks if you
don't need them again. See my earlier posts in the 13 May thread entitled
'Userform question' for more info on a lot of this stuff.

Feel free to ask if you have any questions. Good luck!
--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Gordon Bentley-Mix

Brant,

I trust you understand that the requirement to provide "a summary list of
all the sections at the start of the document to allow users to check the
sections they will require" can be best achieved through the use of a
UserForm containing a series of checkboxes to indicate which sections should
be included in the document. This UserForm would be displayed via an AutoNew
macro (or a Document_New event). The code snippets in my previous post
assumed the existence of this UserForm and a representative checkbox called
'CheckBox1'. This code would be executed as part of the macro that actually
created the document based on the users' input.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top