Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


VBA Module removal for Word Documents  need to clean 700 docs of Macros.

Posted on 2004-10-29
Medium Priority
Last Modified: 2010-05-02
I have a number of documents with an AutoOpen module located under the project Modules.  I want to be able to run a string of code on these documents affecting their appearance (that code is completed now thanks to EE) and then run some code to remove all VBA modules in the Project.  I don't want users to have to click enable macros on opening.  I have tried a number of different solution from the internet and receive errors from all.  Most involve a RemoveAllMacross Sub that I call from a macro; however, I cannot get this approach to work.  
The code I am running is in my global template for this machine and does not affect the end result.  I can go in and delete the Links module just fine and it solves the problem but across 700 docs that could get very time consuming.  With my current approach it will only take an hour to complete if I have to manually delete the module it will take much much longer.   I would like to be able to run the code as a macro from my machine template leaving the Document "Clean'  Thank you in advance.
Question by:jmmahon
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
LVL 50

Accepted Solution

Dave Brett earned 2000 total points
ID: 12451539

The code below will run on all word files in a directory called Myfiles. It will open them and delete all VBA, save them and close the files

I suggest you run it on a test directory first



Option Explicit

Public Sub DeleteAllVBA()
    Dim vbComp As Object, MainFolderName As String
    Dim FSO, Fld, Fil
    Dim WordApp As Document
    Set FSO = CreateObject("Scripting.FileSystemObject")
    MainFolderName = "c:\Myfiles"
    Set Fld = FSO.GetFolder(MainFolderName)
    For Each Fil In Fld.Files
        If Fil.Type = "Microsoft Word Document" Then
            Set WordApp = Documents.Open(Fil.Path)
            For Each vbComp In WordApp.VBProject.VBComponents
                With vbComp
                    If .Type = 100 Then
                        .CodeModule.DeleteLines 1, .CodeModule.CountOfLines
                        WordApp.VBProject.VBComponents.Remove vbComp
                    End If
                End With
            Next vbComp
        End If
End Sub


Author Comment

ID: 12460041
This is awesome, thank you.  One problem there is no longer any code running no macros  visible, yet it still prompts to enable or disable macros is the document.  Any ideas.
LVL 50

Expert Comment

by:Dave Brett
ID: 12460550
No problem :)

I don't know why the phantom macro message pops up - I've duplicated your problem but the document appears to be no different than a brand new file without code.



Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

636 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