ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Unlock VBA Project after getting wrong first time (https://www.excelbanter.com/excel-programming/439736-unlock-vba-project-after-getting-wrong-first-time.html)

Crookie74[_2_]

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?


All times are GMT +1. The time now is 03:41 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com