Trying to show/hide a content control text box based on a user-selected value in a legacy Drop-Down Form

102 views Asked by At

Here's my code

Sub HideText1BasedOnDropdown1()

Dim dropdownValue As String
Dim textFormField As FormField

' Get the value of the Drop-Down Form Field with the bookmark "Dropdown1"
dropdownValue = ActiveDocument.FormFields("Dropdown1").Result

' Check the value of the Drop-Down Form Field and hide or show the Text Form Field accordingly
If dropdownValue = "Yes" Then
    ActiveDocument.ContentControls(Text1).Hidden = True
Else
    ActiveDocument.ContentControls(Text1).Hidden = False
End If

End Sub

The bug highlight's that first ".Hidden" and says:

Compile error: Method or data member not found

I'd rather be using a modern content control drop down box but I read it needs to be a legacy version so I can run my macro on exiting from it.

I think the crux of the problem is signaling the identity of the text box that needs to be hidden. I've also tried to hide a legacy text form field rather than Content Control box with this code, but get essentially the same result:

If dropdownValue = "Yes" Then
        ActiveDocument.FormFields("Text1").Hidden = True
    Else
        ActiveDocument.FormFields("Text1").Hidden = False
1

There are 1 answers

0
Ike On BEST ANSWER

There are two errors:

  • You can't access a specific content control like you do. You can access a CC by its tag-name or by its title. A collection will be returned - in case there are multiple CCs with the same tag or title
  • A content control has no hidden property - but you can set the range to hidden.

ActiveDocument.SelectContentControlsByTag("Text1")(1).Range.Font.Hidden = True

This would work if you have at least one CC with a tag "Text1".