Checkbox to Input AutoText

Anonymous
2020-05-03T20:56:36+00:00

I want to auto change the heading of document to read "AT1" when check box CH1 is checked and for the heading to change to "AT2" and some text (actual 4 rows in a table) further down document (highlighted in the screen grab) to be hidden when check box CH2 is checked.  Only check box CB1 or check box CB2 can be checked but not both. 

I know this can be done using bookmarks and some VB coding but admittedly I am a novice when it comes to VB so am help would be greatly appreciated.

The document is laid out using a table (if that makes a difference).

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

18 answers

Sort by: Most helpful
  1. Jay Freedman 206K Reputation points Volunteer Moderator
    2020-05-03T21:48:31+00:00

    Hiding the four rows is the simplest part of this task. In the template for the form, select the four rows and insert a single bookmark that surrounds them. Let's say you name the bookmark "hideme" (without the quote marks). Then in the VBA code that reacts to the checking of one of the boxes, the statement

    ActiveDocument.Bookmarks("hideme").Range.Font.Hidden = True

    will hide the rows, including their borders and the space they occupy. Be aware that the rows will be visible if someone clicks the ¶ button.

    Similarly, in the template you should insert a bookmark around the AT1 text (assuming that the CH1 box is checked by default). Then the VBA code

        Dim rg As Range

        Set rg = ActiveDocument.Bookmarks("ATnumber").Range

        rg.Text = "AT2"

        ActiveDocument.Bookmarks.Add Name:="ATnumber", Range:=rg

    will change the bookmark's content to AT2 and restore the bookmark (because changing the content removes the bookmark, it has to be re-added). Similar code will change the content back to AT1.

    The remaining parts of the code, making the two check boxes exclusive (checking one will automatically uncheck the other) and knowing where to put the preceding snippets, depends on what kind of check boxes they are:

    What kind do you want to use?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2020-05-04T08:37:50+00:00

    Hi Jay

    Thanks for responding to this (as I was hoping you would having read a previous similar post you responded to but I had little success with replicating).

    I was wanting to keep things simple was thinking the Check Box Control, but are the benefits of using the other options and would it be easier to use a certain one to adapt the VB for other simpler uses (want ever they might be)?

    Would it be possible to draft the full VB once finalised?  Thanks

    0 comments No comments
  3. Jay Freedman 206K Reputation points Volunteer Moderator
    2020-05-04T18:03:40+00:00

    Hi David,

    As with most things in Word, the choices are complicated. Generally, I would recommend using content controls for all the entry "fields" (note, these are not fields in Word's technical jargon).

    There are a few reasons you may not want to use content controls: If the users of the form will have Word for Windows version 2007 or earlier (which don't support check box content controls) or Word for Mac earlier than Office 2016 version 15.23 (see https://answers.microsoft.com/en-us/office/forum/office_2016-word/content-controls-in-word-2016-for-mac). In those cases, you can use legacy form fields.

    I don't recommend ActiveX controls for large forms like this. They're very old technology and not very secure, and they're sometimes ill-behaved. (Ref: https://docs.microsoft.com/en-us/previous-versions/office/developer/office-xp/aa140269(v=office.10)#appropriateness-for-the-task)

    With either content controls or legacy form fields, the VBA code for filling in the form must be different from the ones I showed for using bookmarks. The main difference is that both kinds of those objects have their own "names" that are accessed differently than a bookmark's name.

    For Content Controls:

    For each content control, select it and click the Properties button on the ribbon. In the dialog, enter its "name" in either or both the Title and Tag boxes. Word does not require that each control have a unique name, but the VBA code will be simpler if you do assign unique names. Note also that code referring to these names is case-sensitive, so be consistent in typing the names.

    To "protect" the form, select the text that should be non-editable and all its adjacent content controls, click the Group button on the Developer ribbon, and click Group in the dropdown. To leave free-entry areas such as your Documents Checked, make multiple groups and leave these areas outside any group. (Alternatively, you can put a Rich Text content control in each of those areas and then group the entire form.) Any text that is in a group but not inside a content control will not be editable.

    In the code, getting access to a particular content control is complicated by the fact that the Title and Tag need not be unique. The functions ActiveDocument.SelectContentControlsByTitle and ActiveDocument.SelectContentControlsByTag return an array of content controls instead of a single one. If the titles and tags are unique, the array will have only one entry. If your form contains check boxes with titles CH1 and CH2, then the code

    Dim cc As ContentControl 

    Set cc = ActiveDocument.SelectContentControlsByTitle("CH1")(1)

    will return the CH1 control. 

    Regarding where this code goes: In the macro editor's Project pane, expand your template's project and the Microsoft Word Objects folder under that. Double-click the ThisDocument object. In the code pane on the right, open the dropdown that says "General" and choose "Document". Open the other dropdown and choose ContentControlOnExit. That will insert this pair of lines in the code window:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

    End Sub

    Your code to update the content controls and hide/unhide the table rows goes inside this procedure. The variable named ContentControl passed in as the subroutine's argument will refer to the control that has just been exited, and you can use it to determine what action to take. (Hint: rename that variable to CC to avoid confusion between the variable name and the data type.) Start with an If or Select Case to test whether the control is one that you need to handle; if not, immediately do an Exit Sub.

    To make the check boxes exclusive, use code like this:

    Private Sub Document_ContentControlOnExit( _

        ByVal CC As ContentControl, Cancel As Boolean)

        Dim ccCH1 As ContentControl

        Dim ccCH2 As ContentControl

        Dim ccATnum As ContentControl

        Set ccCH1 = ActiveDocument.SelectContentControlsByTag("CH1")(1)

        Set ccCH2 = ActiveDocument.SelectContentControlsByTag("CH2")(1)

        Set ccATnum = ActiveDocument.SelectContentControlsByTag("ATnumber")(1)

        Select Case CC.Tag ' tag of control being exited

            Case "CH1"

                If CC.Checked Then

                    ccCH2.Checked = False

                    ccATnum.Range.Text = "AT1"

                Else

                    ccCH2.Checked = True

                    ccATnum.Range.Text = "AT2"

                End If

            Case "CH2"

                If CC.Checked Then

                    ccCH1.Checked = False

                    ccATnum.Range.Text = "AT2"

                Else

                    ccCH1.Checked = True

                    ccATnum.Range.Text = "AT1"

                End If

            'Case "some other control's tag"

                ' other code

            Case Else

                Exit Sub

        End Select

    End Sub

    In order to be able to hide rows in the table, those rows will have to be excluded from any grouping.

    For Legacy Form Fields:

    In the Properties dialog of each legacy form field, you should replace the default value in the Bookmark box with the field's name. 

    It's a little backwards, but next you need to write the macro that will handle the value of the check box -- at least type in the Sub statement, even if you're leaving the content of the subroutine empty at this point. Once that's defined, the macro's name will become available in the Exit dropdown in the field's Properties dialog. Select it and click OK. Although you can write a separate macro for each check box, it's more efficient to write one macro that handles both, like the code above. 

    To make the check boxes exclusive, use code like this:

    Sub ExitCH()

        Dim ffCH1 As FormField

        Dim ffCH2 As FormField

        Dim ffATnum As FormField

        Set ffCH1 = ActiveDocument.FormFields("CH1")

        Set ffCH2 = ActiveDocument.FormFields("CH2")

        Set ffATnum = ActiveDocument.FormFields("ATnumber")

        If Selection.FormFields.Count = 1 Then

            Select Case Selection.FormFields(1).Name

                Case "CH1"

                    If ffCH1.CheckBox.Value = True Then

                        ffCH2.CheckBox.Value = False

                        ffATnum.Result = "AT1"

                    Else

                        ffCH2.CheckBox.Value = True

                        ffATnum.Result = "AT2"

                    End If

                Case "CH2"

                    If ffCH2.CheckBox.Value = True Then

                        ffCH1.CheckBox.Value = False

                        ffATnum.Result = "AT2"

                    Else

                        ffCH1.CheckBox.Value = True

                        ffATnum.Result = "AT1"

                    End If

                Case Else

                    Exit Sub

            End Select

        End If

    End Sub

    To be able to hide the four rows, add a bookmark that encloses them, and change the bookmark's .Range.Font.Hidden value to True or False as appropriate. This will work even though the form document is protected for filling in forms.

    1 person found this answer helpful.
    0 comments No comments
  4. Anonymous
    2020-05-05T07:45:36+00:00

    Many thanks for your comprehensive reply.  Unfortunately I am still struggling .  Do I need to include

    1. I am using Content Controls
    2. document is saved as .docm
    3. AT1 and AT2 saved in Autotect Gallery
    4. check boxes CH1 and CH2 have unique titles and tags
    5. complete row in table containing CH1 & CH2 (enclosed by straight lines).  Do I need to protect more or any at all?
    6. Rich Text Control Content (I guested at this) inserted at 'TITLE' (with title and tag set to TITLE) where AT1 / AT2 text should be inserted
    7. bookmark 'HIDE' enclosed rows in table to be hidden when CH2 is checked

    I not sure if I am defining the position of TITLE correctly using 'Rich Text Control Content' (I suspect not) so the text changes to AT1 (with CH1 checked) and AT2 (with CH2 checked) in the document?

    Also regarding the hidden rows when CH2 is checked.  Where does this part (mentioned in your first reply) of the code go? I had a guest in the code below.

    This is the code as currently entered:-

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

        Dim ccCH1 As ContentControl

        Dim ccCH2 As ContentControl

        Dim ccATnum As ContentControl

        Set ccCH1 = ActiveDocument.SelectContentControlsByTag("CH1")(1)

        Set ccCH2 = ActiveDocument.SelectContentControlsByTag("CH2")(1)

        Set ccATnum = ActiveDocument.SelectContentControlsByTag("TITLE")(1)

        Select Case CC.Tag ' tag of control being exited

            Case "CH1"

                If CC.Checked Then

                    ccCH2.Checked = False

                    ccATnum.Range.Text = "AT1"

                    ActiveDocument.Bookmarks("HIDE").Range.Font.Hidden = False

                Else

                    ccCH2.Checked = True

                    ccATnum.Range.Text = "AT2"

                    ActiveDocument.Bookmarks("HIDE").Range.Font.Hidden = True

                End If

            Case "CH2"

                If CC.Checked Then

                    ccCH1.Checked = False

                    ccATnum.Range.Text = "AT2"

                    ActiveDocument.Bookmarks("HIDE").Range.Font.Hidden = True

                Else

                    ccCH1.Checked = True

                    ccATnum.Range.Text = "AT1"

                    ActiveDocument.Bookmarks("HIDE").Range.Font.Hidden = False

                End If

            'Case "some other control's tag"

                ' other code

            Case Else

                Exit Sub

        End Select

    End Sub

    As I previously mentioned I am a beginner :-)

    Thanks in advance

    0 comments No comments
  5. Jay Freedman 206K Reputation points Volunteer Moderator
    2020-05-05T17:34:09+00:00

    There are a couple of things to correct -- not too bad for a first try!

    In the first line of the code, make the change indicated in bold:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

    to

    Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)

    because the variable name CC is used for this in the rest of the macro.

    In the Properties dialogs of the two check box content controls and the content control that you labeled as TITLE, make sure that their names are in the Tag box (the Title box can be left blank) because the code uses the SelectContentControlsByTag function:

    (The Properties dialog of CH2 should be the same as CH1 except for changing "1" to "2".) Also, as shown here, I would recommend using a Plain Text control instead of a Rich Text control. The difference is that a Plain Text control can contain only text, while a Rich Text control can have formatted text, pictures, other content controls, and almost anything you can put into a document. Another thing: There isn't any point in having "AT1" and "AT2" in AutoText; the macro simply puts the appropriate text into the content control.

    You probably don't want the content control's placeholder text ("Click or tap here to enter text") to appear, because users shouldn't be putting their own text there. To remove the placeholder, click the Design Mode button (just above the Properties button). Delete the placeholder text and put in four or five nonbreaking spaces (Ctrl+Shift+spacebar). Click the Design Mode button again to turn it off. The control will appear to be blank until one of the check boxes is checked and the AT number appears.

    If I understand your question in item 5, the answer is that you can group that one line, or more, or none at all. It depends on whether you want to prevent users from altering the text that labels the data boxes (such as "Title of Design") or entering text in boxes that aren't meant to be filled in. As I said earlier, any location that is included in a group but is not inside a content control will not allow typing or formatting. If you do group areas where users should enter things, you must place content controls in those areas so they won't be locked out.

    Grouping is not necessary to enable content controls. (This is different from legacy form fields, which don't work unless the document is protected for filling in fields.) The only purpose of grouping is to prevent changes in areas that need protection.

    0 comments No comments