hello ad9051,
Here is a better version. This does not require adding a reference to the workbook and will automatically unlock your VBA Project, if you have it locked, by supplying the password.
This code removes all macros in the workbook that this macro runs in. The macro deletes itself as well.
Remove All Macros and Unlock VBA Project
'Written: March 08, 2008
'Updated: February 07, 2011
'Author: Leith Ross
'Summary: Removes all macro code and modules from the Active Workbook. Will also
' unlock the VBAProject if it is password protected by supplying the
' password automatically.
Sub RemoveAllMacros()
Dim VBcomp As Object
Dim VBproj As Object
Set VBproj = Application.VBE.ActiveVBProject
'Check if the VBA Project is protected
If VBproj.Protection <> 0 Then
Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
SendKeys "^{TAB}" 'Select the Protection Tab
SendKeys "%P" 'Move cursor to the Password text box
SendKeys "ABC123" & "{ENTER}" 'Send the password
End If
For Each VBcomp In VBproj.VBComponents
Select Case VBcomp.Type
'vbext_ct_StdModule, vbext_ct_ClassModule, vbext_ct_MSForm
Case Is = 1, 2, 3
VBproj.VBComponents.Remove VBcomp
'vbext_ct_Document
Case Is = 100
With VBcomp.CodeModule
.DeleteLines 1, .CountOfLines
End With
End Select
Next VBcomp
End Sub
Bookmarks