【问题描述】如何获取受保护工作表的所有保护选项的值呢?
如何获取下图“保护工作表”窗口中各保护选项的值呢?

【小千解答】借助TLBINF32.DLL库遍历获取Worksheet的Protection对象的所有属性名称,再使用CallByName获取对应的值。


示例代码如下:
Sub xqoffice()
Dim xqIf As InterfaceInfo
Dim xqMem As MemberInfo
Dim xqPar As ParameterInfo
Dim x As Integer
Dim y As Integer
Dim z As Integer
ActiveSheet.Unprotect "xqoffice.cn"
Rows("2:" & Rows.Count).ClearContents
Set xqIf = TLI.InterfaceInfoFromObject(ActiveSheet)
x = 1
For Each xqMem In xqIf.Members
If xqMem.Name = "Protect" Then
x = x + 1
Cells(x, 1).Value = xqMem.Name
y = -1
For Each xqPar In xqMem.Parameters
y = y + 1
Cells(y + x, 2).Value = xqPar.Name
Next xqPar
End If
Next xqMem
With ActiveSheet
[C2].Value = "-"
[C3].Value = .ProtectDrawingObjects
[C4].Value = .ProtectContents
[C5].Value = .ProtectScenarios
[C6].Value = .ProtectionMode
For z = 7 To y + x
Cells(z, 3).Value = CallByName(.Protection, Cells(z, 2).Value, VbGet)
Next z
Cells(z, 2).Value = "EnableSelection"
Cells(z, 3).Value = .EnableSelection
End With
Columns.AutoFit
Set xqPar = Nothing
Set xqMem = Nothing
Set xqIf = Nothing
End Sub运行结果:

注解:如果运行报错“用户定义类型未定义”,则先依次单击【工具】→【引用】,勾选“Typelib infomation”,确定。


【参考资料】