I have been working with the following Sub to delete a module from the ThisWorkbook. When the code is there, it works fine. But if it is not there, I get an error 35 which is fine. I thought by placing error handling code and having the sub exit would work. But it still crashes the sub and throws the error. What am I doing wrong?
I want the seb deleted if it is there and do nothing if it is not.
[vba]Sub DeleteProcedure()
' Delete module
Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim HowManyLines As Long
On Error GoTo ErrorHandler
Set VBCodeMod = ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
With VBCodeMod
StartLine = .ProcStartLine("Workbook_Open", vbext_pk_Proc)
HowManyLines = .ProcCountLines("Workbook_Open", vbext_pk_Proc)
.DeleteLines StartLine, HowManyLines
End With
ErrorHandler:
End Sub[/vba]