1

Could somebody tel me how to unlock the vba project of my protected excel file?

I tried with below C# code:

Exc.wbook = (Excel._Workbook)Exc.Workbooks.Open(FilePath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true);
Exc.VBE.ActiveVBProject = Exc.wbook.VBProject;
Exc.Visible = true;
SendKeys.SendWait("%{F11}^r{TAB}~" + sPrd + "~~%{F11}"); 

This didn't unlock the vbaproject.

I tried with this C# code too....didn't work.

Exc.wbook = (Excel._Workbook)Exc.Workbooks.Open(FilePath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true);
Exc.VBE.ActiveVBProject = Exc.wbook.VBProject;
Exc.Visible = true;
Exc.Unprotect(sPrd );

I have put fake code to follow my comp rules.
Please look into it and tel me what`s wrong with my code.

Community
  • 1
  • 1
user2144293
  • 213
  • 4
  • 10
  • 17
  • Some more reading for you on [Chip Pearson: VBE](http://www.cpearson.com/excel/vbe.htm), [Mr Excel - Unlock VBV Project](http://www.mrexcel.com/forum/excel-questions/59402-code-unprotect-vbaproject.html), [Ozgrid](http://www.ozgrid.com/forum/showthread.php?t=13006) – Our Man in Bananas Mar 07 '13 at 12:56
  • @Philip:Please check my revised post. I`m looking for C# code to unlock my vba project. – user2144293 Mar 07 '13 at 13:08
  • I actually need to add some macro to the excel vb project. So for adding i `ve to unlock right? – user2144293 Mar 07 '13 at 14:09
  • yes, but why at runtime, why not add the new code at design time? – Our Man in Bananas Mar 07 '13 at 14:26
  • Since I dont have excel file with me,i`ll have to add it when it comes to server. So only when I get it then i`ll add it. – user2144293 Mar 07 '13 at 14:48
  • 1
    possible duplicate of [Un Protecting excel VBA Project through SendKeys](http://stackoverflow.com/questions/15272740/un-protecting-excel-vba-project-through-sendkeys) – SeanC Mar 07 '13 at 15:10
  • 1
    You might want to use APIs as shown [HERE](http://stackoverflow.com/questions/16174469/unprotect-vbproject-from-vb-code/16176557#16176557) – Siddharth Rout Apr 24 '13 at 09:55

1 Answers1

0

Unfortunately, VBA does not expose the VBProject passwords in its object model. Consequently, the only way to achieve what you want is via a workaround.

The usual approach is to use SendKeys to simulate the keystrokes that would be used to enter the password for the project. You should be aware, however, that this approach is NOT particularly reliable and I certainly wouldn't want to risk its use in a product that I was distributing or selling, or putting into production anywhere that money or reputation could be affected!

Here is an example of code that I found on Ozgrid Forums (originally written by Bill Manville):

VB:

'need reference To VBA Extensibility 
'need To make sure that the target project Is the active project 
Sub test() 
    UnprotectVBProject Workbooks("ABook.xls"), "password" 
End Sub 

Sub UnprotectVBProject(WB As Workbook, ByVal Password As String) 
' 
' Bill Manville, 29-Jan-2000 
' 
Dim VBP As VBProject, oWin As VBIDE.Window 
Dim wbActive As Workbook 
Dim i As Integer 

Set VBP = WB.VBProject 
Set wbActive = ActiveWorkbook 

If VBP.Protection <> vbext_pp_locked Then Exit Sub 

Application.ScreenUpdating = False 

' Close any code windows To ensure we hit the right project 
For Each oWin In VBP.VBE.Windows 
    If InStr(oWin.Caption, "(") > 0 Then oWin.Close 
Next oWin 

WB.Activate 
' now use lovely SendKeys To unprotect 
Application.OnKey "%{F11}" 
SendKeys "%{F11}%TE" & Password & "~~%{F11}", True 

If VBP.Protection = vbext_pp_locked Then 
    ' failed - maybe wrong password 
    SendKeys "%{F11}%TE", True 
End If 

' leave no evidence of the password 
Password = "" 
' go back To the previously active workbook 
wbActive.Activate 

End Sub 

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
Sub ProtectVBProject(WB As Workbook, ByVal Password As String) 

Dim VBP As VBProject, oWin As VBIDE.Window 
Dim wbActive As Workbook 
Dim i As Integer 

Set VBP = WB.VBProject 
Set wbActive = ActiveWorkbook 

' Close any code windows To ensure we hit the right project 
For Each oWin In VBP.VBE.Windows 
    If InStr(oWin.Caption, "(") > 0 Then oWin.Close 
Next oWin 

WB.Activate 
' now use lovely SendKeys To unprotect 
    Application.OnKey "%{F11}" 
    SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & Password & "{TAB}" & Password & "~" 
    Application.VBE.CommandBars(1).FindControl(Id:=2578, recursive:=True).Execute 
    WB.Save 
End Sub 

Use it at your own risk!

HTH

Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148