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

Posted on 2004-10-29
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
    LVL 50

    Accepted Solution


    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

    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
    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.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Cisco Complete Network Certification Training

    If you’re an IT engineer or technician, it's time you take your career to the next level. This elite training bundle is brimming with all of the information you need to learn to sit for Cisco CNNA, CCNP, and CCENT certification exams.

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    913 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

    18 Experts available now in Live!

    Get 1:1 Help Now