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

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.
Who is Participating?

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

jmmahonAuthor Commented:
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.
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.


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.