|
<p>以下清除活动VBA 工程口令的代码在Excel 2003中无效,不知原因</p><p><br/>'need reference To VBA Extensibility<br/>'need To make sure that the target project Is the active project</p><p>Sub test()<br/>' Workbooks.Open ThisWorkbook.Path & "\test1.xls"<br/> UnprotectVBProject Workbooks("test1.xls"), "password"<br/>End Sub</p><p>Sub UnprotectVBProject(WB As Workbook, ByVal Password As String)<br/>'<br/>' Bill Manville, 29-Jan-2000<br/>'<br/> Dim VBP As VBProject, oWin As VBIDE.Window<br/> Dim wbActive As Workbook<br/> Dim i As Integer</p><p> Set VBP = WB.VBProject<br/> Set wbActive = ActiveWorkbook</p><p> If VBP.Protection <> vbext_pp_locked Then Exit Sub</p><p> Application.ScreenUpdating = False</p><p> ' Close any code windows To ensure we hit the right project<br/> For Each oWin In VBP.VBE.Windows<br/> If InStr(oWin.Caption, "(") > 0 Then oWin.Close<br/> Next oWin</p><p> WB.Activate<br/> ' now use lovely SendKeys To unprotect<br/> Application.OnKey "%{F11}"<br/> SendKeys "%{F11}%TE" & Password & "~~%{F11}", True</p><p> If VBP.Protection = vbext_pp_locked Then<br/> ' failed - maybe wrong password<br/> SendKeys "%{F11}%TE", True<br/> End If</p><p> ' leave no evidence of the password<br/> Password = ""<br/> ' go back To the previously active workbook<br/> wbActive.Activate</p><p>End Sub</p><p>''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br/>Sub ProtectVBProject(WB As Workbook, ByVal Password As String)</p><p> Dim VBP As VBProject, oWin As VBIDE.Window<br/> Dim wbActive As Workbook<br/> Dim i As Integer</p><p> Set VBP = WB.VBProject<br/> Set wbActive = ActiveWorkbook</p><p> ' Close any code windows To ensure we hit the right project<br/> For Each oWin In VBP.VBE.Windows<br/> If InStr(oWin.Caption, "(") > 0 Then oWin.Close<br/> Next oWin</p><p> WB.Activate<br/> ' now use lovely SendKeys To unprotect<br/> Application.OnKey "%{F11}"<br/> SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & Password & "{TAB}" & Password & "~"<br/> Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute<br/> WB.Save<br/>End Sub<br/>''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br/></p> |
|