checkbox to hide entire section

Nenad Ilic
Nenad Ilic used Ask the Experts™
on
Hi guys,
I would need your help. I have a document with several sections (i.e. Services), and the idea is to create checkboxes on start. If we select one service, the section regarding it is shown, if we don't select it, it remains hidden.
Example
Service 1
Service 2
Service 3

Section Service 1 (with content bellow)
Section Service 2 (with content bellow)
etc.

So services that i select with checkbox are visible and the ones that are not, remain hidden. (also it would be good if there is a possibility to keep the sorting, that if I select first Service 2, then Service 1, to still be in the document in line).

I'm quite new in this and I would really appreciate all the help I could get.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
RaminTechnical Advisor

Commented:
Try the attached file, with clicking on the triangle you can also hide everything except the header.
ZZZZ.docx

Author

Commented:
Hi Ramin,
thank you for your reply, but what I was going for is something else.

I've attached a quick example with 2 checkboxes, now, what I need is to "unhide" the service that is been selected, or keep it "hidden" if it's not selected.

Could you please provide me with any possible solution for that?

Kind regards,

Nenad
@Nenad
I've attached a quick example with 2 checkboxes, now, what I need is to "unhide" the service that is been selected, or keep it "hidden" if it's not selected.
No attached file ;-)
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
My bad, forgot to press "upload file" button :)
example.docx

Author

Commented:
Ark, I've looked into that one before, and at the end someone asked the similar question as mine (How would you apply this to multiple sections as well), and it was advised to ask a separate question.
Which led me to a thought that the solution might be bit different.

On the solution https://www.experts-exchange.com/questions/22703028/Macro-to-Display-Hide-Section-of-a-Word-Document.html
GrahamSkan gave a great idea and code, I tried to use it (using his code replacing the "Section2" with "Service1" - from my example) but I did not get it running.
And I've selected entire section as autotext, and followed all the guidelines.

Author

Commented:
The solution definitely doesn't work. If anyone could provide further assistance or help, I would appreciate it
maybe you can find something useful to adapt to your needs here:Toggle Data Display
...

Applications for this type of document might include:
  • A schedule of events with the display of event details hidden unless a user elects to see the details.
  • A procedure might have general steps with an option to display more detailed instructions.
  • Teachers might prepare a set of math problems with the option to display how the problem is worked out, or a set of question with an option to display the answers.

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.
...
http://gregmaxey.com/word_tip_pages/toggle_data_display.html
Try attached sample example1.docm
Split your doc onto sections (first section is checkboxes, others - collapsible regions) -> Page Layout tab, in the Page Setup group, click Breaks, choose Continuous type. Bas module
Private Const SectionsCount = 3 'Collapsible sections count
Dim boxes As Variant 'an array of checkboxes names

Private Sub chkSection1_Click()
   ShowHideSections
End Sub

Private Sub chkSection2_Click()
   ShowHideSections
End Sub

Private Sub chkSection3_Click()
   ShowHideSections
End Sub

Private Sub Document_Open()
   boxes = Array("chkSection1", "chkSection2", "chkSection3") 
   Dim doc As Document, sName As String
   Set doc = ActiveDocument
   With doc.AttachedTemplate.AutoTextEntries
      For i = 2 To 1 + SectionsCount
         sName = "Section" & i
         If Not EntryExists(doc, sName) Then
            .Add sName, doc.Sections(i).Range
         End If
      Next i
   End With
End Sub

Private Function EntryExists(ByVal doc As Document, ByVal name As String) As Boolean
   Dim entry As AutoTextEntry
   On Error Resume Next
   Set entry = doc.AttachedTemplate.AutoTextEntries(name)
   EntryExists = Err = 0
   Err.Clear
   On Error GoTo 0
End Function

Private Sub ShowHideSections()
   Dim rng As Range
   Dim doc As Document
   Set doc = ActiveDocument
   'Delete all sections except first
   For i = 1 To SectionsCount
      doc.Sections(2).Range.Delete
   Next i
'Add section(s) if checkbox checked
   For i = 0 To UBound(boxes)
      If CheckBoxChecked(doc, boxes(i)) Then
         Set rng = doc.Sections(doc.Sections.Count).Range
         rng.Collapse wdCollapseEnd
         doc.AttachedTemplate.AutoTextEntries("Section" & (i + 2)).Insert rng, True
      End If
   Next
End Sub

Private Function CheckBoxChecked(ByVal doc As Document, ByVal name As String) As Boolean
   For Each obj In doc.InlineShapes
        If obj.OLEFormat.Object.name = name Then
            CheckBoxChecked = obj.OLEFormat.Object.Value
            Exit Function
        End If
    Next obj
End Function

Open in new window

Author

Commented:
This did the work, thank you very much for this, I appreciate it

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial