Solved

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

Posted on 2011-02-24
4
307 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

758 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

16 Experts available now in Live!

Get 1:1 Help Now