从下拉列表中提取值,由于表单保护而导致的问题

时间:2018-01-26 06:31:44

标签: vba ms-word word-vba

我有一个按钮,一个遗留下拉表单和一个表格。

当我按下我的按钮时,VBA代码从我的传统下拉表单中提取当前所选项目,并将其插入我的表格。

With ActiveDocument

    .Tables(15).Rows.Last.Cells(1).Range.InsertAfter .FormFields("DropDown2").Result

End With

但是,当我处于非保护模式时,下拉列表不会“下拉”。我需要用户能够从下拉表单中进行选择。

因此,我更改为保护模式以允许下拉列表正常运行。但是,当我在保护模式下运行宏时,我收到此错误:

This method or property is not available because the object refers to a protected area of a document.

我该怎么办?我的方法完全错了,还是有解决方法?

这是User selects item from drop down box, clicks button, and the selected item populates the last row of a table

的后续行动

1 个答案:

答案 0 :(得分:1)

首先,您应该检查文档是否受到保护(听起来会是这样)。然后暂时禁用保护,进行更改,重新启用保护。

With ActiveDocument
    If .ProtectionType <> wdNoProtection Then .Unprotect
    .Tables(15).Rows.Last.Cells(1).Range.InsertAfter .FormFields("DropDown2").Result
    .Protect wdAllowOnlyFormFields
End With

所以请继续保管您的文档,以便下载。这段代码将负责其余部分。