How can i unprotect my VB project from a vb macro ? i have found this code:
Sub UnprotectVBProject(ByRef WB As Workbook, ByVal Password As String)
Dim VBProj As Object
Set VBProj = WB.VBProject
Application.ScreenUpdating = False
'Ne peut procéder si le projet est non-protégé.
If VBProj.Protection <> 1 Then Exit Sub
Set Application.VBE.ActiveVBProject = VBProj
'Utilisation de "SendKeys" Pour envoyer le mot de passe.
SendKeys Password & "~"
SendKeys "~"
'MsgBox "Après Mot de passe"
Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
Application.Wait (Now + TimeValue("0:00:1"))
End Sub
But this solution doesn't work for Excel 2007. It display the authentification's window and print password in my IDE.
Then, my goal is to unprotect my VBproject without displaying this window.
Thanks for any help.
EDIT:
Converted this to a BLOG post for VBA and VB.Net.
I have never been in favor of
Sendkeys
. They are reliable in some case but not always. I have a soft corner for API's though.What you want can be achieved, however you have to ensure that workbook for which you want to un-protect the VBA has to be opened in a separate Excel Instance.
Here is an example
Let's say we have a workbook who's VBA project looks like this currently.
LOGIC:
Find the Handle of the "VBAProject Password" window using
FindWindow
Once that is found, find the handle of the Edit Box in that window using
FindWindowEx
Once the handle of the Edit Box is found, simply use
SendMessage
to write to it.Find the handle of the
Buttons
in that window usingFindWindowEx
Once the handle of the
OK
button is found, simply useSendMessage
to click it.RECOMMENDATION:
For API's THIS is the best link I can recommend.
If you wish to become good at API's like
FindWindow
,FindWindowEx
andSendMessage
then get a tool that gives you a graphical view of the system’s processes, threads, windows, and window messages. For Ex: uuSpy or Spy++.Here is what Spy++ will show you for "VBAProject Password" window
TESTING:
Open a new Excel instance and paste the below code in a module.
CODE: (TRIED AND TESTED)
I have commented the code so you shouldn't have any problem understanding it.
I know you've locked this for new answers but I had a few issues with the above code, principally that I'm working in Office 64-bit (VBA7). However I also made it so the code would work in the current instance of Excel and added a bit more error checking and formatted it up to be pasted into a separate module with only the method
UnlockProject
exposed.For full disclosure I really started with the code in this post although it's a variant on a theme.
The code also shows conditional compilation constants so that it ought to be compatible with both 32-bit and 64-bit flavours of Excel at the same time. I used this page to help me with figuring this out.
Anyways here's the code. Hope someone finds it useful: