Unlock VBA Project after getting wrong first time

C

Crookie74

I need to be able to unlock the VBA Project within my code. I have
used the following code that I found on the net

Private Sub UnprotectVBProject(WB As Workbook, ByVal Password As
String)
Dim vbProj As VBIDE.VBProject

Set vbProj = WB.VBProject

'can't do it if already unlocked!
If vbProj.Protection <> 1 Then Exit Sub

Set Application.VBE.ActiveVBProject = vbProj

' now use lovely SendKeys to quote the project password
SendKeys Password & "~~"

Application.VBE.CommandBars(1).FindControl(ID:=2578,
recursive:=True).Execute

SendKeys "{ESC}"

DoEvents
End Sub


This code works fine when used first time. The problem is, if the user
types the wrong password in, then I call the same routine, it leaves
the Project Properties form on the screen, although it has correctly
unlocked the project.

Can someone please help?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top