0
Hello, I have a spreadsheet whose project has a password, however I want the macros to work and if the user tries to enter the macros he will have to put the password (I put in the project).
I put a code that unlocks the spreadsheet when entering, it’s like this:
Option Explicit
Private Const PAGE_EXECUTE_READWRITE = &H40
Private Declare PtrSafe Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As LongPtr, Source As LongPtr, ByVal Length As LongPtr)
Private Declare PtrSafe Function VirtualProtect Lib "kernel32" (lpAddress As LongPtr, ByVal dwSize As LongPtr, ByVal flNewProtect As LongPtr, lpflOldProtect As LongPtr) As LongPtr
Private Declare PtrSafe Function GetModuleHandleA Lib "kernel32" (ByVal lpModuleName As String) As LongPtr
Private Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As LongPtr, ByVal lpProcName As String) As LongPtr
Private Declare PtrSafe Function DialogBoxParam Lib "user32" Alias "DialogBoxParamA" (ByVal hInstance As LongPtr, ByVal pTemplateName As LongPtr, ByVal hWndParent As LongPtr, ByVal lpDialogFunc As LongPtr, ByVal dwInitParam As LongPtr) As Integer
Dim HookBytes(0 To 5) As Byte
Dim OriginBytes(0 To 5) As Byte
Dim pFunc As LongPtr
Dim Flag As Boolean
Private Function GetPtr(ByVal Value As LongPtr) As LongPtr
GetPtr = Value
End Function
Public Sub RecoverBytes()
If Flag Then MoveMemory ByVal pFunc, ByVal VarPtr(OriginBytes(0)), 6
End Sub
Public Function Hook() As Boolean
Dim TmpBytes(0 To 5) As Byte
Dim p As LongPtr
Dim OriginProtect As LongPtr
Hook = False
pFunc = GetProcAddress(GetModuleHandleA("user32.dll"), "DialogBoxParamA")
If VirtualProtect(ByVal pFunc, 6, PAGE_EXECUTE_READWRITE, OriginProtect) <> 0 Then
MoveMemory ByVal VarPtr(TmpBytes(0)), ByVal pFunc, 6
If TmpBytes(0) <> &H68 Then
MoveMemory ByVal VarPtr(OriginBytes(0)), ByVal pFunc, 6
p = GetPtr(AddressOf MyDialogBoxParam)
HookBytes(0) = &H68
MoveMemory ByVal VarPtr(HookBytes(1)), ByVal VarPtr(p), 4
HookBytes(5) = &HC3
MoveMemory ByVal pFunc, ByVal VarPtr(HookBytes(0)), 6
Flag = True
Hook = True
End If
End If
End Function
Private Function MyDialogBoxParam(ByVal hInstance As LongPtr, ByVal pTemplateName As LongPtr, ByVal hWndParent As LongPtr, ByVal lpDialogFunc As LongPtr, ByVal dwInitParam As LongPtr) As Integer
If pTemplateName = 4070 Then
MyDialogBoxParam = 1
Else
RecoverBytes
MyDialogBoxParam = DialogBoxParam(hInstance, pTemplateName, hWndParent, lpDialogFunc, dwInitParam)
Hook
End If
End Function
This code executes when opening the spreadsheet (https://stackoverflow.com/questions/1026483/is-there-a-way-to-crack-the-password-on-an-excel-vba-project?rq=1), however when I run another macro, Excel points out that the spreadsheet is locked and will not run.
Would anyone have a macro to assist me in this? Maybe Keycode. Or a Userform.
That’s the mistake that happens:
I think I misunderstood... the macros code is usually protected, not executed. When you put a password in the code so the user can’t see the vba project this does not prevent the macro execution. What should be preventing execution is that your spreadsheet is blocked, no?
– Evert
Exactly what should happen, but when I run for example the
Before Save
he points out an error. I edited my question and put the error, give a look.– Leandro Felipe Moreira
But why are you wanting crack your own spreadsheet? I confess that I am confused... I believe that you do not need this code there in your own spreadsheet.
– Evert
It was just an attempt to unlock and lock again. The big problem is not working code.
– Leandro Felipe Moreira
Brother... I suggest we try to make everything run without blocking anything, remove all that has lock, if everything is working, start thinking about what you need to block... if it’s just the vba code you won’t need any code for it... just go to the settings and enter your password.
– Evert
That’s the kk question. I just blocked the VBA code
– Leandro Felipe Moreira
and stopped working?
– Evert
Let’s go continue this discussion in chat.
– Evert