Excel VBA - 检查工作表是否受密码保护
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40874165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA - Check if a worksheet is protected WITH A PASSWORD
提问by D. O.
We can check if a sheet is protected using ProtectContents property. But how check if it is protected with a password?
我们可以使用 ProtectContents 属性检查工作表是否受到保护。但是如何检查它是否受密码保护?
if ws.ProtectContents then
''do something
end if
回答by JNevill
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
You can call this like:
你可以这样称呼它:
isSheetProtectedWithPassword(Worksheets("Sheet1"))
And it will return True
or False
它会返回True
或False