我有一個工作簿,它處理一系列輸入工作簿,其中一些已設置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項目
- 2. 將項目密碼傳遞給鎖定的VBA項目
- 3. 用vba解鎖一個vba項目,知道密碼?
- 4. 加密首選項/屬性文件中的密碼 - Java
- 5. 比較(密碼)屬性
- 6. Neo4j檢查屬性密碼
- 7. OpenDJ密碼重用屬性
- 8. MVC密碼屬性散列
- 9. Log4PostSharp項目屬性
- 10. Maven項目屬性
- 11. eclipse項目屬性
- 12. 項目屬性值
- 13. 其他屬性的Maven密碼加密
- 14. 如何從VBA項目中刪除密碼?
- 15. VBA項目密碼保護與SendKeys無法正常工作
- 16. VBProject工具 - > VBA項目Propeties密碼保護
- 17. 在VBA密碼框中加密密碼
- 18. 密碼python項目編號
- 19. vba將模塊vba項目中的代碼轉移到excel表單vba項目
- 20. 在開源項目中加密密碼
- 21. VBA - 行屬性
- 22. Typescript visual Studio項目屬性
- 23. Visual Studio項目屬性
- 24. ASP DropDown項目屬性
- 25. C#項目屬性錯誤
- 26. xamarin android項目屬性
- 27. Visual C++項目屬性@ $%宏
- 28. POM /項目屬性MAVEN2
- 29. 部署項目屬性
- 30. Kony項目屬性窗口
嗨加里 - 對不起,但這是不正確的。使用Excel 2010提供密碼而不勾選框允許訪問vb代碼,但在檢查屬性時請求密碼 - 我遇到過這種情況的10個工作簿 - 易於演示。 –
謝謝加里 - 我會研究你的代碼,但重點是可以編輯vb代碼,但任何嘗試檢查屬性都會導致密碼輸入框。很奇怪,您無法複製這個問題,因爲我在我檢查的300多本工作簿中的10箇中遇到過這個問題。我的家庭和辦公室版本的Excel都可以創建複選框未勾選但存在密碼的情況。如果有幫助,我可以向您發送一個示例工作簿。 –
嘗試過的代碼 - 說沒有鎖定,但右鍵單擊VBA項目屬性要求輸入密碼。 –