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. Anonymous
    2020-05-10T11:33:31+00:00

    Many thanks again Jay.  Its up and running without error :-)

    It is possible to force/restrict the input value of F and S (left and right) to a value of 1 to 5 and logic RF <= LF?

    Also is it possible to prevent deletion of the content control but allow the unused table rows to be deleted?

    On a separate task (independent table to the recent posts) are you familiar with using multiple Drop-down List Conetent Control where say you have 3 lists in a table, List A in cell a1, List B in cell b1 and List C in cell c1.  List C contents is dependent on List B selection and List B contents is dependent and List A selection?  Again I have this working in Excel and struggling in Word again :-(

    0 comments No comments
  2. Jay Freedman 206K Reputation points Volunteer Moderator
    2020-05-11T00:59:12+00:00

    The complete macro is at the end of this section of the post, but first I'll describe the added parts.

    To restrict the values of F and S to integers from 1 to 5, add this sequence just after setting ccF through ccL:

                CC.Range.Text = Int(Val(CC.Range.Text))

                If Val(CC.Range.Text) < 1 Or Val(CC.Range.Text) > 5 Then

                    MsgBox "Value must be from 1 to 5", vbCritical, "Oops!"

                    Cancel = True

                    Exit Sub

                End If

    The Val function converts text to a numeric data type; if the text doesn't start with a digit, the function returns 0. The Int function converts any number with a decimal part to just the integer part. Thus, the first line makes sure that the content of CC is an integer. If that integer is out of the range, then the message appears, and the cursor doesn't leave the content control.

    Making sure that RF <= LF is a little more complicated. For one thing, the code in the second half of the macro has to get the LF value, which would not have been retrieved in this execution of the macro (the first Case was skipped). So the second Case adds the lines

                Dim ccLF As ContentControl

                Set ccLF = ActiveDocument.SelectContentControlsByTag("LF" & intRow)(1)

    Then the verification of the 1-to-5 range is done as in the first Case, followed by the test for RF <= LF:

                If CC.Tag = ccF.Tag And Val(ccLF.Range.Text) < Val(ccF.Range.Text) Then

                    MsgBox "Value must be less than or equal to " & ccLF.Range.Text, vbCritical, "Oops!"

                    Cancel = True

                    Exit Sub

                End If

    Here's the complete macro:

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

        Dim ccCH1 As ContentControl

        Dim ccCH2 As ContentControl

        Dim ccATnum As ContentControl

        Dim ccF As ContentControl

        Dim ccS As ContentControl

        Dim ccR As ContentControl

        Dim ccL As ContentControl

        Dim intRow As Integer

        Dim intVal As Integer

        Dim rg As Range

        Select Case True

            Case (CC.Tag Like "LF*") Or (CC.Tag Like "LS*") ' exiting control in left side of table

                ' get the row number from the tag

                intRow = Right(CC.Tag, Len(CC.Tag) - 2)

                Set ccF = ActiveDocument.SelectContentControlsByTag("LF" & intRow)(1)

                Set ccS = ActiveDocument.SelectContentControlsByTag("LS" & intRow)(1)

                Set ccR = ActiveDocument.SelectContentControlsByTag("LR" & intRow)(1)

                Set ccL = ActiveDocument.SelectContentControlsByTag("LL" & intRow)(1)

                CC.Range.Text = Int(Val(CC.Range.Text))

                If Val(CC.Range.Text) < 1 Or Val(CC.Range.Text) > 5 Then

                    MsgBox "Value must be from 1 to 5", vbCritical, "Oops!"

                    Cancel = True

                    Exit Sub

                End If

                ' calculate value for LR control

                intVal = Val(ccF.Range.Text) * Val(ccS.Range.Text)

                ccR.Range.Text = intVal

                Set rg = ccR.Range.Duplicate

                'include cell marker, thus whole cell

                rg.MoveEnd wdCharacter, 2

                If intVal < 5 Then

                    rg.Shading.BackgroundPatternColor = wdColorOliveGreen

                    ccL.Range.Text = "Low, Broadly Acceptable"

                ElseIf intVal <= 9 Then

                    rg.Shading.BackgroundPatternColor = wdColorLightOrange

                    ccL.Range.Text = "Medium, Tolerable"

                Else

                    rg.Shading.BackgroundPatternColor = wdColorRed

                    ccL.Range.Text = "High, Unacceptable"

                End If

                ' make RF control's value same as LF control

                ActiveDocument.SelectContentControlsByTag("RF" & intRow)(1).Range.Text = _

                    ccF.Range.Text

            Case (CC.Tag Like "RF*") Or (CC.Tag Like "RS*") ' exiting control in right side of table

                ' get the row number from the tag

                intRow = Right(CC.Tag, Len(CC.Tag) - 2)

                Set ccF = ActiveDocument.SelectContentControlsByTag("RF" & intRow)(1)

                Set ccS = ActiveDocument.SelectContentControlsByTag("RS" & intRow)(1)

                Set ccR = ActiveDocument.SelectContentControlsByTag("RR" & intRow)(1)

                Set ccL = ActiveDocument.SelectContentControlsByTag("RL" & intRow)(1)

                Dim ccLF As ContentControl

                Set ccLF = ActiveDocument.SelectContentControlsByTag("LF" & intRow)(1)

                CC.Range.Text = Int(Val(CC.Range.Text))

                If Val(CC.Range.Text) < 1 Or Val(CC.Range.Text) > 5 Then

                    MsgBox "Value must be from 1 to 5", vbCritical, "Oops!"

                    Cancel = True

                    Exit Sub

                End If

                If CC.Tag = ccF.Tag And Val(ccLF.Range.Text) < Val(ccF.Range.Text) Then

                    MsgBox "Value must be less than or equal to " & ccLF.Range.Text, vbCritical, "Oops!"

                    Cancel = True

                    Exit Sub

                End If

                ' calculate value for RR control

                intVal = Val(ccF.Range.Text) * Val(ccS.Range.Text)

                ccR.Range.Text = intVal

                Set rg = ccR.Range.Duplicate

                'include cell marker, thus whole cell

                rg.MoveEnd wdCharacter, 2

                If intVal < 5 Then

                    rg.Shading.BackgroundPatternColor = wdColorOliveGreen

                    ccL.Range.Text = "Low, Broadly Acceptable"

                ElseIf intVal <= 9 Then

                    rg.Shading.BackgroundPatternColor = wdColorLightOrange

                    ccL.Range.Text = "Medium, Tolerable"

                Else

                    rg.Shading.BackgroundPatternColor = wdColorRed

                    ccL.Range.Text = "High, Unacceptable"

                End If

            Case Else

                Exit Sub

        End Select

    End Sub

    =========================================

    If a row contains content controls that have been set to "Content control cannot be deleted", Word won't allow that row to be deleted. First the option would have to be turned off in each control. Of course, that's too much work to be reasonable :) so here's a macro that will turn off the option in all the controls in the selected rows and then delete the rows.

    Sub DeleteTableRowsWithControls()

        Dim oRow As Row

        Dim cc As ContentControl

        If Not Selection.Information(wdWithInTable) Then

            MsgBox "Select rows to delete."

            Exit Sub

        End If

        If MsgBox("Delete selected rows?", vbYesNo) = vbYes Then

            For Each oRow In Selection.Rows

                For Each cc In oRow.Range.ContentControls

                    cc.LockContentControl = False

                Next cc

            Next oRow

            Selection.Rows.Delete

        End If

    End Sub

    =========================================

    For your last request, the answer is more complicated than I can write up here. First take a look at https://gregmaxey.com/word_tip_pages/link_content_to_dropdown_list.html. After that, if you have questions about how to handle your particular application, please start a new thread here. I'll keep an eye out for your post.

    0 comments No comments
  3. Jay Freedman 206K Reputation points Volunteer Moderator
    2020-05-11T17:10:31+00:00
    0 comments No comments