Solved

Macro that will remove all macros from a project in Excel 2003 and/or 2007

Posted on 2011-02-24
4
313 Views
Last Modified: 2012-05-11
I need a code that will remove all macros from a project in 2003 and/or 2007. I have the code below, but it requires "Microsoft Visual Basic for Applications Extensibility library" and i can't find it in the exhaustive References list. So unless there's a simpler code that works, exactly how far down the Ecel 2003 References list is the extensibility library reference?

Thanlks,
John
0
Comment
Question by:gabrielPennyback
  • 2
4 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 300 total points
ID: 34974954
The routine below, PurgeCodeInWorkbook, removes all source code from a workbook. The routine will not work on the workbook in which it is located and running. The following sample code illustrates how to save a copy of a workbook and use the routine to purge all VBA code from it.

[Begin Code Segment]

   Const FileName = "C:\Full\Path\To\Workbook Copy.xls"
   ThisWorkbook.SaveCopyAs FileName
   Workbooks.Open FileName, False
   PurgeCodeInWorkbook ActiveWorkook
   ActiveWorkbook.Save
   ActiveWorkbook.Close False

[End Code Segment]

The routine for purging the VBA source code follows below.

[Begin Code Segment]

Public Sub PurgeCodeInWorkbook( _
      ByVal TargetWorkbook As Workbook _
   )

' Purge all VBA code in the specified workbook. The specified workbook must not
' be in the workbook in which this code is running.
   
   Dim VBComponent As Variant
   Dim VBCodeModule As CodeModule
   
   If TargetWorkbook Is ThisWorkbook Then Stop ' Can't delete code in the same workbook
   
   For Each VBComponent In TargetWorkbook.VBProject.VBComponents
      Set VBCodeModule = VBComponent.CodeModule
      If VBCodeModule.CountOfLines > 0 Then
         If VBComponent.Type = 1 Then
            TargetWorkbook.VBProject.VBComponents.Remove VBComponent
         Else
            VBCodeModule.DeleteLines 1, VBCodeModule.CountOfLines
         End If
      End If
   Next VBComponent

End Sub

[End Code Segment]

Kevin
0
 
LVL 41

Assisted Solution

by:dlmille
dlmille earned 200 total points
ID: 34975275
Of course, a very simple way to purge code from a workbook is to save it as a macro-free workbook.

File SaveAs ExcelWorkbook or Exce 97-2003 Workbook.

Its that simple.  I had never done this before so I took a sample excel file with lots of macros in several modules and sheet codepages, saved as macro free and presto - the job was done.

Dave
0
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 300 total points
ID: 34975924
>exactly how far down the Ecel 2003 References list is the extensibility library reference?

The list is in alphabetical order. Scroll down to "Microsoft V..." and it will be there.

Kevin
0
 
LVL 1

Author Closing Comment

by:gabrielPennyback
ID: 35033578
Thanks!

John
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now