
Originally Posted by
John_G
How was the editor locked in the first place? Are you able to open a form in design view and see the code (View code on the ribbon)?
If you are using an accde file, you can't see or update the code anyway.
Hello,
I locked it through the VBA editor. I have the password and I'm able to edit manually. However, my goal is to use a command button to access the VBA editor by entering it through code. Meaning, I press the button the code than opens the editor and enters my password in the API that prompts for it, "VBAproject Password". Below is the code that I have so far and its not working. I can't get it to input the password in the API that prompts the password.
Code:
Option Explicit
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 hWndParent As Long, _
ByVal hwndChildAfter As Long, _
ByVal lpszClass As String, _
ByVal lpszWindow As String) _
As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal Msg As Long, _
wParam As Any, lParam As Any) _
As Long
Private Declare Function SetActiveWindow Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Sub openVBA_Click()
VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
Dim hDialog As Long
Dim hTextBox As Long
Dim hButton As Long
Const WM_SETTEXT = &HC
Const BM_CLICK = &HF5
Const cPASSWORD As String = "123"
'// Try to find the Project password dialogue
hDialog = FindWindow(vbNullString, "VBAProject Password")
'// If NOT found then Exit
If (hDialog = 0) Then
MsgBox "Project Password dialog not found...", vbExclamation, "Error"
Exit Sub
End If
'// Check if the Text is found in the dialog - The Class ('EDIT', 'BUTTON') names are found using SPY
'// or similar. These are constant and have not changed since Windows 1!!
hTextBox = FindWindowEx(hDialog, 0, "Edit", "")
'// If the textbox is NOT found then Exit
If (hTextBox = 0) Then
MsgBox "Couldn't find the textbox in the window " & CStr(hDialog) & "...", vbExclamation, "Error"
Exit Sub
End If
'// Send text to the textbox
SendMessage hTextBox, WM_SETTEXT, ByVal 0, ByVal cPASSWORD
'// Now get a handle to the "OK" button in the dialog.
hButton = FindWindowEx(hDialog, 0, "BUTTON", "OK")
If hButton = 0 Then
MsgBox "Unable to find the 'OK' button on the Project Password dialog...", vbExclamation, "Error"
Else
'// After making sure that the dialog box is the active window, click "OK".
'// This does not mean it is topmost, simply that it will receive any input
SetActiveWindow hDialog
SendMessage hButton, BM_CLICK, ByVal CLng(0), ByVal CLng(0)
End If
End Sub