We help IT Professionals succeed at work.
Get Started

How to get Word VBA macro to compile a VBA project?

Last Modified: 2008-03-24
I'm using the attached procedure below to delete all the lines of code from a VBA, to ensure the saved document will not run code again. On opening the saved document getting "System Error &H8000040005(-214746259).
Once the saved document is manually opened and the VB editor recompiled(even though no code exists), this error will no longer affect the document.

Sub DeleteAllVBACode_WhenSaving()
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        On Error GoTo error_trap
        Set VBProj = ActiveDocument.VBProject
        Application.ScreenUpdating = False
        For Each VBComp In VBProj.VBComponents
            If VBComp.Type = vbext_ct_Document Then
                Set CodeMod = VBComp.CodeModule
                With CodeMod
                    .DeleteLines 1, .CountOfLines
                End With
                VBProj.VBComponents.Remove VBComp
            End If
        Next VBComp
Exit Sub
MsgBox Err.Number & vbCrLf & Err.Description, vbOKOnly + vbCritical, "Error in CleanUp"
    End Sub

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE