View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Anthony Cuttitta Jr. Anthony Cuttitta Jr. is offline
external usenet poster
 
Posts: 8
Default Unprotecting a VBA Project

I had to do some digging recently to try and find the answer to this,
and while I did find a couple good leads, the code wasn't *quite*
right. So, for the benefit of someone else trying to attempt to do
this crazy thing, I thought I'd post the code here. (Really, it's NOT
an ego thing...just trying to give back a little.)

NOTE: The constant "conPW " is the password to be used on the project.
The value is stored with the project at save/close, so locking the
doors when you leave isn't necessary.


Public Sub UnprotectVBAProject()
'This has been tested in E97 only.
On Error GoTo ErrHandler
Const conPW as String = "MyPassword"

'Open VBE
Call SendKeys("%{F11}", True)
'Open Project Explorer
Call SendKeys("%(V)P", True)
'PageUp 5 times to be sure "VBAProject" is selected
Call SendKeys("{PGUP 5}", True)
'Hit Enter to give prompt, enter PW and Enter again.
Call SendKeys("{ENTER}" & conPW & "{ENTER}", True)

ExitProcedu
Exit Sub

ErrHandler:
Select Case Err.Number
Case Else
Application.ScreenUpdating = True
MsgBox Err.Number & vbNewLine & Err.Description,
vbCritical
Resume ExitProcedure
Resume
End Select

End Sub