Forum Discussion

Keldyding's avatar
Keldyding
Copper Contributor
Jan 12, 2022

Adaptive behavior in a form?

HI My problem: I have a form made in Word, that my colleagues have to fill out relative often, at the beginning of a new task. The form is pretty long because it has to to be flexible enough to me...
  • Doug_Robbins_Word_MVP's avatar
    Jan 14, 2022

    Keldyding 

    To make the code a bit simpler, I would suggest that each section of the form be in a separate Section of the document - Form section 1 in Document Section 2, Form section 2 in Document Section 3, --- Form section 8 in Document Section 9, Form section 9 in Document Section 10, and you have the Nine check boxes in Section 1, with the Titles of the Check boxes being Section 1, Section 2, --- Section 8, Section 9.

    Then, in Visual Basic Editor in the ThisDocument object insert the following code

    Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
    Dim Secnum As Long
    For Each CC In ActiveDocument.ContentControls
        If Left(CC.Title, 7) = "Section" Then
            Secnum = Mid(CC.Title, 9)
            If CC.Checked = True Then
                ActiveDocument.Sections(Secnum + 1).Range.Font.Hidden = False
            Else
                ActiveDocument.Sections(Secnum + 1).Range.Font.Hidden = True
            
            End If
        End If
    Next
    End Sub

    Then, if you have the display of Hidden text disabled (Via File>Display) when you check any of the text boxes and exit from the text box, the code will format the text of the Section of the document for which the associated checkbox is checked so that the font is not hidden and for any Section for which the associated checkbox is not checked so that it is hidden and will not therefore appear.

     

    Unfortunately, while a macro enabled workbook (xlsm) can be attached to a response, a macro enabled Word document or template cannot be attached.  If you contact me at dougrobbinsmvp[atsymbol]gmail[dot]com, I will send you the macro enabled template that I created to respond to your question. 

Resources