2013-03-07 79 views
1

有人能告訴我如何解鎖我受保護的excel文件的vba項目嗎?如何從C#中的excel文件解鎖受保護的vbaproject?

我試着用下面的C#代碼:

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}"); 

這並沒有解開的VBAProject。

我試着用這個C#代碼太....沒有工作。

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); 

我已經把假的代碼按照我的補償規則。
請仔細檢查一下,並告訴我我的代碼有什麼問題。

+0

你在[芯片皮爾森:VBE一些更多的閱讀(http://www.cpearson.com/excel/vbe.htm),[Excel的先生 - 解鎖VBV項目(HTTP:/ /www.mrexcel.com/forum/excel-questions/59402-code-unprotect-vbaproject.html), [Ozgrid](http://www.ozgrid.com/forum/showthread.php?t=13006) – 2013-03-07 12:56:35

+0

@菲利普:請檢查我修改後的帖子。我正在尋找C#代碼來解鎖我的vba項目。 – user2144293 2013-03-07 13:08:28

+0

我其實需要在excel vb項目中添加一些宏。所以爲了增加我的解鎖權? – user2144293 2013-03-07 14:09:33

回答

0

不幸的是,VBA不會在其對象模型中公開VBProject密碼。因此,通過解決方法來實現您想要的唯一方法。

通常的做法是使用SendKeys來模擬將用於輸入項目密碼的擊鍵。但是,您應該意識到,這種方法並不是特別可靠,我當然不希望在我分配或銷售的產品中使用它,或者在可能影響金錢或聲譽的任何地方投入生產。

下面是代碼,我在Ozgrid Forums發現(最初是由比爾·曼維爾寫的)一個例子:

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 

您自擔風險使用它!

HTH

相關問題