尝试根据旧版下拉表单中用户选择的值显示/隐藏内容控制文本框

问题描述 投票:0回答:1

这是我的代码

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

bug 亮点是第一个“.Hidden”并表示:

编译错误: 未找到方法或数据成员

我宁愿使用现代内容控制下拉框,但我读到它需要是旧版本,以便我可以在退出时运行我的宏。

我认为问题的关键在于表明需要隐藏的文本框的身份。我还尝试使用此代码隐藏旧文本表单字段而不是内容控制框,但得到的结果基本相同:

If dropdownValue = "Yes" Then
        ActiveDocument.FormFields("Text1").Hidden = True
    Else
        ActiveDocument.FormFields("Text1").Hidden = False
vba ms-word contentcontrol
1个回答
0
投票

有两个错误:

  • 您无法像您一样访问特定的内容控件。您可以通过其标签名称或标题来访问 CC。如果有多个具有相同标签或标题的 CC,将返回一个集合
  • 内容控件没有隐藏属性 - 但您可以将范围设置为隐藏。

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

如果您至少有一个带有标签“Text1”的 CC,则此方法可行。

© www.soinside.com 2019 - 2024. All rights reserved.