vbams-wordcontentcontrol

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


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

Debug highlights that first .Hidden and says:

Compile error:
Method or data member not found

I'd rather use 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 to be hidden. I 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

Solution

  • There are two errors:

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

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