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?