Using Send Keys to unprotect and protect VB project

G

Gareth

I have a file which needs some code amending/ replacing. The project is
protected so I need to unprotect it before making any changes. I have
searhed this group and it would appear that SendKeys is the easiest way to
do it.

So far I have the following code, the unprotect part works fine but I am
unable to protect it again afterwards.

What I intend to do is have two xl files open, the fix file and the one to
be fixed. I will run the macro from the fix file.

Can anyone see where I'm going wrong?

Sub TestUnprotect()
Application.Windows(2).Activate
UnprotectVBProject ActiveWorkbook, "Gareth"
'update code, etc
End Sub

Sub TestProtect()
Application.Windows(2).Activate
ProtectVBProject ActiveWorkbook, "Gareth"
End Sub

Sub UnprotectVBProject(WB As Workbook, ByVal Password As String)
Dim vbproj As Object
Set vbproj = WB.VBProject
Set Application.VBE.ActiveVBProject = vbproj
SendKeys Password & "~~"
Application.VBE.CommandBars(1).FindControl(Id:=2578,
recursive:=True).Execute
End Sub

Sub ProtectVBProject(WB As Workbook, ByVal Password As String)
Dim vbproj As Object
Set vbproj = WB.VBProject
Set Application.VBE.ActiveVBProject = vbproj
SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & Password & "{TAB}" & Password & "~"
Application.VBE.CommandBars(1).FindControl(Id:=2578,
recursive:=True).Execute
WB.Save
End Sub

I have no idea how the code works, just got it from a previous post.

Thanks in advance.
 

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