Excel VBA-检查工作表是否受密码保护 [英] Excel VBA - Check if a worksheet is protected WITH A PASSWORD

查看:137
本文介绍了Excel VBA-检查工作表是否受密码保护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用ProtectContents属性检查工作表是否受到保护.但是如何检查它是否受密码保护?

We can check if a sheet is protected using ProtectContents property. But how check if it is protected with a password?

if ws.ProtectContents then
    ''do something
end if 

推荐答案

我不认为有直接的方法可以通过属性来实现.但是,您也可以尝试使用空白密码取消保护工作表,并在失败时捕获错误:

I don't believe there is a direct way of doing this by way of a property. Alternatively, though, you could attempt to unprotect the worksheet with a blank password and catch the error should it fail:

Function isSheetProtectedWithPassword(ws As Worksheet) As Boolean
    If ws.ProtectContents Then
        On Error GoTo errorLabel
        ws.Unprotect ""
        ws.Protect
    End If
errorLabel:
    If Err.Number = 1004 Then isSheetProtectedWithPassword = True
End Function

您可以这样称呼:

isSheetProtectedWithPassword(Worksheets("Sheet1"))

它将返回 True False

这篇关于Excel VBA-检查工作表是否受密码保护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆