×

保护 protect drawingobjects scenarios userinterfaceonly

获取受保护工作表的所有保护选项的值

鹭岛小千 鹭岛小千 发表于2021-10-03 11:29:50 浏览621 评论0

抢沙发发表评论

【问题描述】如何获取受保护工作表的所有保护选项的值呢?

如何获取下图“保护工作表”窗口中各保护选项的值呢?

47-1.png

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

47-2.png47-3.png

示例代码如下:

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

运行结果:

47-4.png


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

47-5.png


打赏码.png


【参考资料】

  1. Worksheet.Protect 方法 (Excel)

  2. 如何保護Excel表格上的圖像

  3. Excel Scenarios

  4. UserInterfaceOnly怎么运用的?


群贤毕至

访客