重新保护Excel工作表,启用自动筛选器

我正在使用VB.net 2005将数据库应用程序中的数据input到Excel文件中。

我不保护纸张,使用命名范围将数据input到特定的单元格中,然后重新保护纸张。

到目前为止,这一直运行良好,直到我碰到一个受启用自动过滤保护的文件。 当我重新保护床单,我似乎无法保护它,同时允许使用自动筛选器下拉列表。 自动筛选器的下拉箭头将变灰并禁用。

input数据后使用下面的代码重新保护纸张。

If bolProtected = True Then For i = 0 To intProtectIndexes.Length - 1 objExcel.Sheets(intProtectIndexes(i)).Select() objExcel.ActiveSheet.Protect(strPassword) Next End If 

也尝试使用Tim的链接代码。

 objExcel.Sheets(intProtectIndexes(i)).Select() objExcel.ActiveSheet.Protect(Password:=strPassword, Contents:=True) objExcel.activesheet.enableautofilter = True 

这仍然使填充的工作表上禁用了自动筛选器下拉菜单。

在VBA中创buildmacros并使用:

 ActiveSheet.Protect(Contents:=True, AllowFiltering:=True) 

工作正常。 工作表受到保护,我可以使用自动筛选器下拉菜单。 但是,当我在我的VB.net项目中使用相同的代码,下拉没有启用。

尝试

 With objExcel.Sheets(intProtectIndexes(i)) .Protect(Password:=strPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True, UserInterfaceOnly:=True) .EnableAutoFilter = True End With 

要么

 With objExcel.Sheets(intProtectIndexes(i)) .Protect (Password:=strPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True) End With