0
votes

I am trying to create a form that requires text boxes to be disabled until a checkbox is ticked. Previously I have been able to use activeX and Legacy tools in the document but after a software upgrade to a 3rd party application the manufacture no longer supports these. I am therefore having to re-learn how to use content control boxes and their VBA controls.

This is my attempt based at varying google responses. "Lowers" is the checkbox. "Long3""Long4" "Lat2""Vert2" are the textboxes I am trying to control.

I had managed to disable the textboxes but this now seems to have been lost.

Any suggestions would be greatly appreciated.

Private Lowers_onChange()
Dim oCC_Lowers As ContentControl
Set oCC_Lowers = ActiveDocument.SelectContentControlsByTag("Lowers").Item(1)

Dim oCC_Long3 As ContentControl
Set oCC_Long3 = ActiveDocument.SelectContentControlsByTag("Long3").Item(2)
Dim oCC_Long4 As ContentControl
Set oCC_Long4 = ActiveDocument.SelectContentControlsByTag("Long4").Item(3)
Dim oCC_Lat2 As ContentControl
Set oCC_Lat2 = ActiveDocument.SelectContentControlsByTag("Lat2").Item(4)
Dim oCC_Vert2 As ContentControl
Set oCC_Vert2 = ActiveDocument.SelectContentControlsByTag("Vert2").Item(5)

If oCC_Lowers.LockcontentControl = True Then
oCC_Long3.LockContents = False
oCC_Long4.LockContents = False
oCC_Lat2.LockContents = False
oCC_Vert2.LockContents = False

Else

oCC_Long3.LockContents = True
oCC_Long4.LockContents = True
oCC_Lat2.LockContents = True
oCC_Vert2.LockContents = True


End If
End Sub
1

1 Answers

0
votes

Firstly a link to a good resource on working with Content Controls: Greg Maxey's Word Content Controls page

As your code is an event handler responding to a built-in event, it needs to be in the ThisDocument module. As there isn't a Change event you need to use the OnExit event. You can find this by navigating to the ThisDocument module in the VBE and selecting Document in the left hand drop-down. In the right hand drop-down select ContentControlOnExit. An empty event handler is then created with the correct syntax.

enter image description here

You will note that this event handler will fire for every content control in the document, so you need to test the value of ContentControl.Tag to see if it is the one that you need to work with.

Once you have verified that you have the correct content control you can then use the value of its Checked property to set the value of the LockContents property of each of the other content controls. Your completed code should then look like this:

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
  If ContentControl.Tag = "Lowers" Then
    Dim oCC_Long3 As ContentControl
    Set oCC_Long3 = ActiveDocument.SelectContentControlsByTag("Long3").Item(1)
    oCC_Long3.LockContents = Not ContentControl.Checked
    Dim oCC_Long4 As ContentControl
    Set oCC_Long4 = ActiveDocument.SelectContentControlsByTag("Long4").Item(1)
    oCC_Long4.LockContents = Not ContentControl.Checked
    Dim oCC_Lat2 As ContentControl
    Set oCC_Lat2 = ActiveDocument.SelectContentControlsByTag("Lat2").Item(1)
    oCC_Lat2.LockContents = Not ContentControl.Checked
    Dim oCC_Vert2 As ContentControl
    Set oCC_Vert2 = ActiveDocument.SelectContentControlsByTag("Vert2").Item(1)
    oCC_Vert2.LockContents = Not ContentControl.Checked
  End If
End Sub