View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Crookie74[_2_] Crookie74[_2_] is offline
external usenet poster
 
Posts: 1
Default Unlock VBA Project after getting wrong first time

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:=257 8,
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?