0

The following VBA macro is simply available as a against incorrect email transmission in Outlook365.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim aExtAddrs() As String ' External Email Addresses
    Dim iExtNum As Integer    ' Number of those addresses
    Dim olRecip
    iExtNum = -1
    For Each olRecip In Item.Recipients
        If Not olRecip.Address Like "*@mydomain" Then
            iExtNum = iExtNum + 1
            ReDim Preserve aExtAddrs(iExtNum)
            aExtAddrs(iExtNum) = olRecip.Address
        End If
    Next
    If iExtNum < 0 Then Exit Sub
    If MsgBox("Contains external email addresses. Are there any mistake ?" & vbCrLf & _
              Join(vbCrLf, aExtAddrs), vbYesNo + vbQuestion, "Comfirmation") <> vbYes Then
        Cancel = True ' Cancel to send
        Exit Sub
    End if
End Sub

So made this an add-in as it is.

Public Class ThisAddIn
    Private Sub ThisAddIn_Startup() Handles Me.Startup
        AddHandler Me.Application.ItemSend, AddressOf CatchItemSend
    End Sub

    Public Sub CatchItemSend(ByVal Item As Object, ByRef Cancel As Boolean)
        Dim aExtAddrs() As String ' External Email Addresses
        Dim iExtNum As Integer    ' Number of those addresses
        iExtNum = -1
        For Each olRecip As Outlook.Recipient In Item.Recipients
            If Not olRecip.Address Like "*@mydomain" Then
                iExtNum = iExtNum + 1
                ReDim Preserve aExtAddrs(iExtNum)
                aExtAddrs(iExtNum) = olRecip.Address
            End If
        Next
        If iExtNum < 0 Then Exit Sub
        If MsgBox("Contains external email addresses. Are there any mistake ?" & vbCrLf & _
                  Join(vbCrLf, aExtAddrs), vbYesNo + vbQuestion, "Comfirmation") <> vbYes Then
            Cancel = True ' Cancel to send
            Exit Sub
        End if
    End Sub
End Class

In this case, if you don't remove the VBA macro when you install the add-in, the ItemSend event handler will be set twice and both will be called (simultaneously or sequentially). So I want to disable VBA macros, but I don't know how to do it properly. The simple thing is to manually remove the VBA macros when installing the add-in, but I don't want it, I want to do it automatically. What kind of method is possible?

  • Does this help answer your question [How to control Macro Settings using registry keys or GPOs](https://www.heelpbook.net/2016/how-to-control-macro-settings-using-registry-keys-or-gpos/)? Just set the registry keys during installation of your add-in. But be aware that users might change that and it would cause troubles again. So better to remove that macro otherwise you might offend a lot of users. – Pᴇʜ Dec 17 '20 at 13:30
  • Thanks for your comment. The way you adviced will suppress the entire VBA macros of Outlook, but I don't want it, I only want suppress the Application_ItemSend enent handler. Is there an option other than manually remove the part of the VBA macro ? – Masanori Watase Dec 17 '20 at 14:47
  • Ah, got it. I guess there is no way to do that programmatically (for security reasons). While you can edit VBA code programmatically in Word and Excel this does not work with Outlook (see [here](https://stackoverflow.com/a/58496250)). All Outlook macros are saved in `%appdata%\Microsoft\Outlook\VbaProject.OTM` so you could delete that file but it would delete **all** Outlook macros! You cannot edit that file as it is a proprietary fomat. – Pᴇʜ Dec 17 '20 at 15:04
  • 1
    Thank you for following in detail. I understood. The only way is to delete the macro manually... – Masanori Watase Dec 17 '20 at 23:39

0 Answers0