Last active
August 29, 2015 14:13
-
-
Save mcgr0g/67a7d5a9d8eb10bba18c to your computer and use it in GitHub Desktop.
mine method to reuse http://stackoverflow.com/a/16176557
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'================= | |
'protection module | |
'================= | |
Option Explicit | |
'need for unLockVba | |
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ | |
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long | |
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _ | |
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _ | |
ByVal lpsz2 As String) As Long | |
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _ | |
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long | |
Private Declare Function GetWindowTextLength Lib "user32" Alias _ | |
"GetWindowTextLengthA" (ByVal hwnd As Long) As Long | |
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ | |
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long | |
Dim Ret As Long, ChildRet As Long, OpenRet As Long | |
Dim strBuff As String, ButCap As String | |
'Virtual Key Codes | |
Const WM_SETTEXT = &HC | |
Const BM_CLICK = &HF5 | |
Private Const secret4proj = "ololo" | |
'<some code>' | |
'##################### | |
'VBAProject protection | |
'##################### | |
'----------------------------------------------- | |
'hepler for testing | |
'you can add button cmnTest to WorkSheet and use | |
'cmdTest_Click | |
'----------------------------------------------- | |
Sub test() | |
unLockVba ActiveWorkbook | |
End Sub | |
'--------------------------------------------- | |
' based on http://stackoverflow.com/a/16176557 | |
'--------------------------------------------- | |
Sub unLockVba(oWb As Workbook) | |
'Dim xlAp As Object | |
Dim wbPath, winHeader As String | |
Dim proj As VBProject | |
On Error GoTo errHandler: | |
Set proj = oWb.VBProject | |
Debug.Print "trying to unlock " & proj.name | |
If oWb.VBProject.protection = vbext_pp_none Then | |
Debug.Print "у книги не защищенный проект" | |
GoTo exitHere: | |
End If | |
'> Launch the VBA Project Password window | |
SendKeys "%{F11}" | |
SendKeys "^r" | |
SendKeys proj.name | |
DoEvents | |
proj.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute | |
'proj.VBE.CommandBars("Menu Bar").Controls("Tools").Controls(proj.name & " Properties...").Execute | |
'> Get the handle of the "VBAProject Password" Window | |
winHeader = proj.name & " Password" ' for projName = "specialProj" | |
Ret = FindWindow(vbNullString, winHeader) | |
If Ret = 0 Then | |
Debug.Print winHeader & " window was not Found" | |
Else | |
Debug.Print winHeader & " window found" | |
'> Get the handle of the TextBox Window where we need to type the password | |
ChildRet = FindWindowEx(Ret, ByVal 0&, "Edit", vbNullString) | |
If ChildRet = 0 Then | |
Debug.Print "The Edit Box was not found" | |
Else | |
Debug.Print "TextBox's window found" | |
'> This is where we send the password to the Text Window | |
SendMess secret4proj, ChildRet | |
DoEvents | |
'> Get the handle of the Button's "Window" | |
ChildRet = FindWindowEx(Ret, ByVal 0&, "Button", vbNullString) | |
'> Check if we found it or not | |
If ChildRet = 0 Then | |
Debug.Print "Button's Window Not Found" | |
Else | |
Debug.Print "Button's window found" | |
'> Get the caption of the child window | |
strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0)) | |
GetWindowText ChildRet, strBuff, Len(strBuff) | |
ButCap = strBuff | |
'Loop through all child windows | |
Do While ChildRet <> 0 | |
'Check if the caption has the word "OK" | |
If InStr(1, ButCap, "OK") Then | |
'If this is the button we are looking for then exit | |
OpenRet = ChildRet | |
Exit Do | |
End If | |
'Get the handle of the next child window | |
ChildRet = FindWindowEx(Ret, ChildRet, "Button", vbNullString) | |
'Get the caption of the child window | |
strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0)) | |
GetWindowText ChildRet, strBuff, Len(strBuff) | |
ButCap = strBuff | |
Loop | |
'> Check if we found it or not | |
If OpenRet = 0 Then | |
Debug.Print "The Handle of OK Button was not found" | |
Else | |
'> Click the OK Button | |
SendMessage ChildRet, BM_CLICK, 0, vbNullString | |
End If | |
End If | |
End If | |
End If | |
exitHere: | |
Set proj = Nothing | |
Exit Sub | |
errHandler: | |
handleError "[UnlockVBA]" ' great thing, Check https://github.com/mcgr0g/vbaDeveloper/blob/master/src/vbaDeveloper.xlam/ErrorHandling.bas | |
End Sub | |
'----------------------------- | |
'sends virtual keys to windows | |
'----------------------------- | |
Sub SendMess(Message As String, hwnd As Long) | |
Call SendMessage(hwnd, WM_SETTEXT, False, ByVal Message) | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment