Removing VBA Modules in Excel doesn't succeed.

I'm trying to create a subroutine in the Excel 2002 VB Editor to automatically remove the module once all the script has finished running (trying to automate a daily process using Excel Macros that run when the worksheet is opened). My goal is to save the resulting sheet as a new sheet, and not have the macros follow it, thus bringing up the "Enable Macros/Disable Macros" warning that's featured in Office XP.

My Code is this:

Private Sub Auto_Open()
    ' MsgBox "Running Workbook_Open . . ."
    Dim sTest As String
    sTest = Range("A2").Value
    If sTest = "" Then
    End If
End Sub
Private Sub dataExtract()
    ' MsgBox "Running dataExtract . . ."
    ' Create a connection object.
    Dim cnPubs As ADODB.Connection
    Set cnPubs = New ADODB.Connection

    ' Provide the connection string.
    Dim strConn As String

    'Use the SQL Server OLE DB Provider.
    strConn = "PROVIDER=SQLOLEDB;"

    'Connect to the Pubs database on the local server.
    strConn = strConn & "DATA SOURCE=[Server];INITIAL CATALOG=[pubs];"

    'Use an integrated login.
    strConn = strConn & " INTEGRATED SECURITY=sspi;"

    'Now open the connection.
    cnPubs.Open strConn

    ' Create a recordset object.
    Dim rsPubs As ADODB.Recordset
    Set rsPubs = New ADODB.Recordset
    Dim dtYesterday As String
    Dim dtToday As String

    dtToday = DatePart("w", Date)
    If dtToday = "2" Then
        dtYesterday = Date - 3
        dtYesterday = Date - 1
    End If
    ' MsgBox dtYesterday
    With rsPubs
        ' Assign the Connection object.
        .ActiveConnection = cnPubs
        ' Extract the required records.
        .Open "Select e.programno, e.programdate, c.LName, c.FName, c.PaxId from EnrollmentsHist e, Customer c where (e.XActDate >= '" & dtYesterday & "') AND (c.PaxID = e.PaxID) AND (e.LastXAct = 'C') AND (e.ClientID in ('eh', 'ehd', 'rs'))"
        ' Copy the records into cell A1 on Sheet1.
        Sheet1.Range("A2").CopyFromRecordset rsPubs

        ' Tidy up
    End With

    Set rsPubs = Nothing
    Set cnPubs = Nothing


End Sub

Private Sub sortData()

    Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range("B2") _
        , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, _
End Sub
Sub DeleteThisModule()

    MsgBox "Hi, I will delete myself "
    ActiveWorkbook.VBProject.VBComponents.Remove ActiveWorkbook.VBProject.VBComponents("Module2")

End Sub

I don't get any errors, and once the marcos are done running the code window in the VB Editor closes, but the module isn't actually removed. I've also tried these sets of code in Sub DeleteThisModule(), but had the same lack of results:

Dim VBComp As VBIDE.VBComponent
Dim VBComps As VBIDE.VBComponents
Set VBComps = ActiveWorkbook.VBProject.VBComponents
For Each VBComp In VBComps
   Select Case VBComp.Type
      Case vbext_ct_StdModule, vbext_ct_MSForm, vbext_ct_ClassModule
        MsgBox VBComps
         VBComps.Remove VBComp
      Case Else
         With VBComp.CodeModule
            .DeleteLines 1, .CountOfLines
         End With
   End Select
Next VBComp


 Dim vbCom As Object
    Set vbCom = Application.VBE.ActiveVBProject.VBComponents
    vbCom.Remove VBComponent:= _

I have the "Tools > Macro > Security..." "Trust access to Visual Basic Project" checked already.
I have MS VB for Applications Extensibility 5.3, MS ActiveX Data Objects Recordset 2.8 Library, MS ActiveX Data Objects 2.8 Library, XLODBC.XLA, MS Office 10.0 Object Library, OLD Automation, MS Excel 10.0 Object Library, and Visual Basic for Applications all loaded from Tools > References in MS VB Editor.

500 Points because a quick solution would be best, also this seems difficult enough to merit them.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hello, Vriaeliss,

I tried copying just the "delete module" section of your code (i.e. the last three lines) into a Macro.  It worked for me in XL2000.

Perhaps there is a difference with XL2002, or perhaps something else in your extended macro is interfering.  You might try commenting-out sections of code to test this second hypothesis.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VriaelissAuthor Commented:
Hmm... That's true. It does work in a new sheet (the delete module sub alone).

I ended up developing a work-around that made a new sheet to store the data, and I think that works better than this idea (delete the module and save the current workbook as a new file).

I wonder if it has anything to do with the delete module sub being at the end of a chain that runs automatically upon opening...

Since I've worked around the problem, I'd like to close the question, so if you've worked on this, post what you have and I'll divvy points best I can :)

 ~ Beau
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.