none
Can you use a Checkbox control to toggle hidden text in a Word 2010 document? RRS feed

  • Question

  • I want to toggle the "hidden" property on a block of text using the CheckBox user control. In the Word document, the Show/Hide button is presumed selected so the user can see hidden content on their screen.

    So, for example:

    This is paragraph 1. This is a standard paragraph that readers will always see.

    [object:CHECKBOX1] This is paragraph 2. Paragraph 2 is "hidden" UNTIL CheckBox1 is clicked, thereby turning off the HIDDEN property.

    Basically, the client wants a list of items, each with a checkbox. The reader will check which items (at least one, maybe all) they want to display. When the document is printed, only the paragraphs with checked boxes (...oh yeah, the checkboxes have to keep their HIDDEN property, too...) will display.

    At first, it appears simple. Insert Control Object, add paragraph, select Font > Hidden property. BUT, I am not clear on how I add code to the page that says "if you click this box, then the associated text property of Hidden is turned for this particular paragraph. (and there will be several of these paragraphs to choose from...)

    Suggestions? Thoughts?

    Paragraph 1: This is a paragraph of text that starts out HIDDEN, but I want to see it when the checkbox is clicked.

    Paragraph 2: This is a paragraph of text that starts out HIDDEN, but I want to see it when the checkbox is clicked.

    Paragraph 3: This is a paragraph of text that starts out HIDDEN, but I want to see it when the checkbox is clicked.

    Monday, March 9, 2015 5:10 PM

All replies

  • You cannot do this without macros and you cannot force users to run macros, but if that is not an issue you could use something like then following.

    In the 'ThisDocument' module of the document add the following code:

    Option Explicit
    
    Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
        HideByCheckBox
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
        HideByCheckBox
    lbl_Exit:
        Exit Sub
    End Sub
    

    Add an ordinary module to the document and insert the following code

    Option Explicit
    
    Sub HideByCheckBox()
    Dim oCC As ContentControl
    Dim oRng As Range
        For Each oCC In ActiveDocument.ContentControls
            If oCC.Type = wdContentControlCheckBox Then
                Set oRng = oCC.Range.Paragraphs(1).Range
                oRng.End = oRng.End - 1
                oRng.Start = oRng.Start + 3
                If oCC.Checked = True Then
                    oRng.Font.Hidden = False
                Else
                    oRng.Font.Hidden = True
                End If
            End If
        Next oCC
    lbl_Exit:
        Exit Sub
    End Sub
    
    Sub FilePrintDefault()
    Dim oCC As ContentControl
    Dim oRng As Range
    Dim oPara As Range
        For Each oCC In ActiveDocument.ContentControls
            If oCC.Type = wdContentControlCheckBox Then
                Set oRng = oCC.Range
                oRng.Font.Hidden = True
            End If
        Next oCC
        Options.PrintHiddenText = False
        ActiveDocument.PrintOut
        For Each oCC In ActiveDocument.ContentControls
            If oCC.Type = wdContentControlCheckBox Then
                Set oRng = oCC.Range
                oRng.Font.Hidden = False
                Set oPara = oCC.Range.Paragraphs(1).Range
                oPara.End = oPara.End - 1
                oPara.Start = oPara.Start + 3
                If oPara.Font.Hidden = False Then
                    oCC.Checked = False
                End If
            End If
        Next oCC
    lbl_Exit:
        Exit Sub
    End Sub
    

    The macro will show/hide the paragraph associated with the check when the cursor is moved out of the content control, or if another content control is selected

    When the document is printed using the FilePrintDefault button on the QAT (Quick Access Toolbar) the checkboxes also are hidden, the document is printed and the checkboxes and values restored.

    Personally I don't like this approach. It is much more elegant to create a template with VBA userform with either the check boxes and their values listed, or a multi-select list box with the paragraphs listed, then the document is created with the selected items positioned as required. This leaves fewer opportunities for users to screw it up.


    Graham Mayor - Word MVP
    www.gmayor.com

    Tuesday, March 10, 2015 9:09 AM