0

Can I delete an Outlook VBA Module?

I have tried the below code:

Dim vbMod As Object

Set vbMod = Application.VBE.ActiveVBProject.VBComponents
vbMod.Remove VBComponent:=vbMod.Item("Module2")

But getting an error:

438 Error, Object doesn't support this property or method

Can this be done with Outlook VBA and are there any References to be included?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

2 Answers2

1

Try this.

You will need to add a reference to Microsoft Visual Basic for Applications Extensibility 5.3.

Public Sub DeleteModule(ByVal ModuleName As String)
    On Error GoTo Trap

    Dim VBAEditor As VBIDE.VBE
    Dim objProject As VBIDE.VBProject
    Dim objComponent As VBIDE.VBComponent

    Set VBAEditor = Application.VBE
    Set objProject = VBAEditor.ActiveVBProject

    For Each objComponent In objProject.VBComponents
        If objComponent.Name = ModuleName Then
            objComponent.Collection.Remove objComponent
        End If
    Next

Leave:
    On Error GoTo 0
    Exit Sub

Trap:
    MsgBox Err.Description, vbCritical
    Resume Leave
End Sub

To test it:

Sub Test()
    DeleteModule "ModuleName"
End Sub
Kostas K.
  • 8,293
  • 2
  • 22
  • 28
1

The answer to your answer is no, we can't delete or even access programmatically the VBIDE; it is correct that you can add reference to Microsoft Visual Basic for Applications Extensibility 5.3, but to no avail.

If you try this at Word or Excel, this is the output:

enter image description here

But, when you try this at Outlook, VBE is not exposed:

enter image description here

Here is a confirmation. Maybe in older Outlook versions, less safer, you could do that, but at least since Outlook 2002, it is not possible.