我有一个工作簿,它处理一系列输入工作簿,其中一些已设置VBA密码但未锁定以供查看 - 即无需密码即可导航vb代码,但需要密码才能查看项目属性(例如工具/参考)。在这种情况下,即使设置了密码,VBProject.Protection也会设置为vbext_pp_none。我可以检查什么来检测“查看项目属性的密码”是否存在?vba项目属性密码
回答
当您保护该项目时,您必须勾选该框并提供密码。
- 如果没有如果你打勾的框,供您将提示您添加密码的密码才能继续
换句话说滴答作响的保护不被应用于
vbext_pp_none
(0)或vbext_pp_locked
(1)。 编辑/补充: -
阅读您的意见后,我无法重现的情况,但在所有版本/平台,我无法想象这是不可能的。下面是一个例子,通过这个例子,一个属性在错误捕获过程中试图改变,如果它成功了,那么它根本就没有被锁定。
Public Sub Sample()
Dim WkBk As Workbook
Set WkBk = Application.Workbooks.Open(Environ("UserProfile") & "\Desktop\Book1.xlsm")
If WkBk.VBProject.Protection = 1 Then 'vbext_pp_locked then
MsgBox "It is locked"
Else
If LockedForEdits(WkBk) Then
MsgBox "It is locked for edits"
Else
MsgBox "It is not locked"
End If
End If
WkBk.Close 0
Set WkBk = Nothing
End Sub
Private Function LockedForEdits(ByRef WkBk As Workbook) As Boolean
Dim StrDescription As String
On Error GoTo ErrorHandle
StrDescription= WkBk.VBProject.Description
WkBk.VBProject.Description = WkBk.VBProject.Description & "TEST"
WkBk.VBProject.Description = StrDescription
Exit Function
ErrorHandle:
Err.Clear
LockedForEdits = True
End Function
下面的代码依赖于不存在问题,但如果使用Excel 2010在PC上专门工作(测试)或2007(未测试),它会自动侦测出密码的存在,并与代码一起信息你已经有了并且在上一个答案中编码,它应该回答检测密码存在方式的问题。
最新的办公文件格式是zip包,为此您可以将其从.xlsm
重命名为.zip
并查看其内容。在zip包中,xl
文件夹中可能有bin
文件(如果文件中没有VBA,则不存在)。在bin
文件中有一个名为'DPB'的字符串值,该值被加密,但如果有密码,则该值很长,因此可以通过'DPB'值的长度检测到密码的存在。
下面的代码将受益于重要的错误处理,因为有很多文件操作发生,并且如前所述,这与前一个答案中代码的更改版本一起使用,应提供答案题。
下面的代码需要添加'Windows Scripting Runtime'引用(Tools> References> tick'Windows Scripting Runtime'),我没有迟到绑定使它更快更清晰。我也评论整个代码来描述发生了什么
Public Sub Sample()
Dim FSO As New FileSystemObject
Dim Shl As Object
Dim Fl As Scripting.File
Dim Fldr As Scripting.Folder
Dim LngCounter As Long
Dim Ts As Scripting.TextStream
Dim StrTmpFldr As String
Dim StrWkBk As String
Dim StrWkBkName As String
Dim StrContainer As String
Dim WkBk As Excel.Workbook
'A place to work with temp files, for my own ease I done it on the desktop
'but this is not good practice
StrTmpFldr = Environ("UserProfile") & "\Desktop\"
'A path to a workbook (may be passed in)
StrWkBk = Environ("UserProfile") & "\Desktop\Book4.xlsm"
'We need the file name seperate from the path
StrWkBkName = Right(StrWkBk, Len(StrWkBk) - InStrRev(StrWkBk, "\"))
'Copy the workbook and change it to a .zip (xlsx, and other new forms are zip packages)
FSO.CopyFile StrWkBk, StrTmpFldr & Left(StrWkBkName, InStrRev(StrWkBkName, ".")) & "zip", True
'Create a folder to extract the zip to
FSO.CreateFolder StrTmpFldr & Left(StrWkBkName, InStrRev(StrWkBkName, ".") - 1)
'Unzip it into the folder we created
Set Shl = CreateObject("Shell.Application")
Shl.Namespace(StrTmpFldr & Left(StrWkBkName, InStrRev(StrWkBkName, ".") - 1) & "\").CopyHere Shl.Namespace(StrTmpFldr & Left(StrWkBkName, InStrRev(StrWkBkName, ".")) & "zip").Items
Set Shl = Nothing
'Delete the zip
FSO.DeleteFile StrTmpFldr & Left(StrWkBkName, InStrRev(StrWkBkName, ".")) & "zip", True
Set Fldr = FSO.GetFolder(StrTmpFldr & Left(StrWkBkName, InStrRev(StrWkBkName, ".") - 1) & "\xl\")
'Is there a project file? (there won't be if there is no code in it)
For Each Fl In Fldr.Files
If Right(Fl.Name, 4) = ".bin" Then Exit For
Next
If Fl Is Nothing Then
MsgBox "It is not protected"
Else
'Parse the file looking for the line starting "DPB="" if the value in here is over 25 long,
'then it is storing a password
Set Ts = Fl.OpenAsTextStream(ForReading)
Do Until Ts.AtEndOfStream
StrContainer = Ts.ReadLine
If Left(StrContainer, 5) = "DPB=" & """" Then
StrContainer = Replace(Replace(StrContainer, "DPB=", ""), """", "")
If Len(StrContainer) > 25 Then
MsgBox "It is protected"
Else
MsgBox "It is not protected"
End If
Exit Do
End If
Loop
Ts.Close
Set Ts = Nothing
Set Fl = Nothing
End If
Set Fldr = Nothing
'Delete the folder
FSO.DeleteFolder StrTmpFldr & Left(StrWkBkName, InStrRev(StrWkBkName, ".") - 1), True
End Sub
你是否最终解决了这个问题?这个答案有帮助吗? –
- 1. 用vba解锁一个vba项目,知道密码?
- 2. MVC密码属性散列
- 3. Neo4j检查属性密码
- 4. Log4PostSharp项目属性
- 5. 项目属性值
- 6. VBProject工具 - > VBA项目Propeties密码保护
- 7. 如何从VBA项目中删除密码?
- 8. 在VBA密码框中加密密码
- 9. 密码python项目编号
- 10. vba将模块vba项目中的代码转移到excel表单vba项目
- 11. 在开源项目中加密密码
- 12. POM /项目属性MAVEN2
- 13. Nhibernate项目到类属性
- 14. xamarin android项目属性
- 15. 部署项目属性
- 16. ASP DropDown项目属性
- 17. C#ContextMenuStrip项目属性!
- 18. 未更新项目属性
- 19. Kony项目属性窗口
- 20. Authlogic和密码和密码确认属性 - 无法访问?
- 21. 无法编辑项目VBA代码
- 22. 微软项目研究VBA代码
- 23. 组随机值,密码属性
- 24. 保护属性文件中的密码
- 25. ASP.Net C#CreateUserWizard用户名,密码属性
- 26. ActiveDirectory中的密码的属性(cn = nameuser)
- 27. Rails - 未知的属性密码
- 28. 失败-section 7 - “未知属性:密码”
- 29. 不支持的属性*密码
- 30. 忘记了密码VBA
嗨加里 - 对不起,但这是不正确的。使用Excel 2010提供密码而不勾选框允许访问vb代码,但在检查属性时请求密码 - 我遇到过这种情况的10个工作簿 - 易于演示。 –
谢谢加里 - 我会研究你的代码,但重点是可以编辑vb代码,但任何尝试检查属性都会导致密码输入框。很奇怪,您无法复制这个问题,因为我在我检查的300多本工作簿中的10个中遇到过这个问题。我的家庭和办公室版本的Excel都可以创建复选框未勾选但存在密码的情况。如果有帮助,我可以向您发送一个示例工作簿。 –
尝试过的代码 - 说没有锁定,但右键单击VBA项目属性要求输入密码。 –