Macro to Delete Macros

Radical Dylan

New Member
Joined
Nov 21, 2011
Messages
6
I am writing a Macro that essentially makes a copy of the spreadsheet I am working on, removes all the macros in the background, and then saves it to a specific folder.
The entire macro works perfectly on excell 2003, but in 2007 and 2010 there is an error in the section below. Does anyone have any ideas why? All help is appreciated, I had been trying to figure it out by myself all weekend.
Code:
    Dim vFilename As Variant
    Dim wbActiveBook As Workbook
    Dim VBComp As VBIDE.VBComponent
    Dim VBComps As VBIDE.VBComponents
 
    'Get a filename to save as
    'FolderName, Project, and Residence are all variables set earlier in the code.
    ChDrive FolderName
    ChDir FolderName
    vFilename = FolderName & "\" & project & "_PI Sheet - " & residence & ".xls"
 
    ActiveWorkbook.SaveCopyAs vFilename
    Set wbActiveBook = Workbooks.Open(vFilename)
 
    Set VBComps = wbActiveBook.VBProject.VBComponents
 
    For Each VBComp In VBComps
       Select Case VBComp.Type
          Case vbext_ct_StdModule, vbext_ct_MSForm, _
                vbext_ct_ClassModule
             VBComps.Remove VBComp
          Case Else
             With VBComp.CodeModule
                .DeleteLines 1, .CountOfLines
             End With
       End Select
    Next VBComp
 
Why not just save the workbook as an xlsx? That will strip the VBA project.
 
Upvote 0
When I step through the code, it errors out on this line:
Code:
Set VBComps = wbActiveBook.VBProject.VBComponents

Giving me a:
Run-time Error "1004"
Application-Defined or Object-Defined Error
 
Upvote 0
Well, that was it. Thanks!
I feel slightly stupid now, I had done that on my 2003, but not my other two version. I forgot and slightly assumed it would automatically do so as long as I was using the same spreadsheet. I guess that isn't the case.

Thanks again!
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top