Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-24
4
Medium Priority
?
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 1200 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 42

Assisted Solution

by:dlmille
dlmille earned 800 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 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

721 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