Delete a Module in Outlook Project

前端 未结 2 507
慢半拍i
慢半拍i 2021-01-26 09:41

Can I delete an Outlook VBA Module?

I have tried the below code:

Dim vbMod As Object

Set vbMod = Application.VBE.ActiveVBProject.VBComponents
vbMod.Rem         


        
相关标签:
2条回答
  • 2021-01-26 10:18

    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.

    0 讨论(0)
  • 2021-01-26 10:27

    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
    
    0 讨论(0)
提交回复
热议问题