Search code examples
vbams-wordtextinputdisabled-input

Disable TextInput Object


I have a checkbox and if the checkbox.Value = False I want to disable my TextInput-Object. On the internet there where some suggestions, but the methods which were used are not working for me, because the methods are not found.

I tried it with the .Valid-method:

Dim tf As TextInput
Dim checkbox As CheckBox

Sub checkbox_click()

Set checkbox = ActiveDocument.FormFields("checkbox").CheckBox
Set tf = ActiveDocument.FormFields("textfield").TextInput

If checkbox.Value = False

    tf.Valid = False
End If

End Sub

But that doesn't work for some reason. I found tf.Enabled = False on the internet, but this method is unknown in my case.


Solution

  • You need something more like this:

    Dim ff As FormField
    Dim checkbox As CheckBox
    .
    .
    Set checkbox = ActiveDocument.FormFields("checkbox").CheckBox
    Set ff = ActiveDocument.FormFields("textfield")
    
    If checkbox.Value = False
      ff.Enabled = False
    End If
    

    With legacy FormField objects, some of the properties you need are associated with the FormField itself, and others are associated with child objects of the FormField such as the FormField.Checkbox

    So the problem here is that tf is a FormField.TextInput object, but .Enabled is a property of the FormField object.

    Not relevant to your question, but just as an observation, FormFields do not have Word events associated with them in the normal sense of VBA Events. The settings of each field tell Word to run a named Sub "on entry" and/or "on exit" - that's it. No actual click events. No problem with using names that makes those things look like events but I just thought I'd mention it.