Delete a Module in Outlook Project

半世苍凉 提交于 2019-12-02 07:57:42

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

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:

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!