How to get warning when sending email to external domain in Outlook?
While using Outlook, how can you prevent sending emails accidentally to recipients outside your company? Actually, you can display warning box when sending email to external domain in Outlook. This article will introduce a VBA method to achieve it.
Get warning when sending email to external domain with VBA code
Get warning when sending email to external domain with VBA code
The following VBA code can help you get warning when sending email to external domain in Outlook. Please do as follows.
1. Press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.
2. In the Microsoft Visual Basic for Applications window, double click ThisOutlookSession to open the ThisOutlookSession (Code) window, and then copy below VBA code into the window. See screenshot:
VBA code: Get warning when sending email to external domain
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Update by Extendoffice 20180504
Dim xMailItem As Outlook.MailItem
Dim xRecipients As Outlook.Recipients
Dim i As Long
Dim xRecipientAddress As String
Dim xPrompt As String
Dim xYesNo As Integer
Dim xPos As Integer
On Error Resume Next
If Item.Class <> olMail Then Exit Sub
Set xMailItem = Item
Set xRecipients = xMailItem.Recipients
For i = xRecipients.Count To 1 Step -1
xRecipientAddress = xRecipients.Item(i).Address
xPos = InStrRev(LCase(xRecipientAddress), "@addin99.com")
If xPos <= 0 Then Exit For
Cancel = False
Next
If InStrRev(LCase(xRecipientAddress), "@addin99.com") > 0 Then Exit Sub
xPrompt = "Are you sure to send this email to outside your company? "
xYesNo = MsgBox(xPrompt, vbYesNo + vbQuestion, "Kutools for Outlook")
If xYesNo = vbNo Then Cancel = True
End Sub
Note: In the code, “@addin99.com” is the internal email domain you will send emails to. Please change it as you need.
From now on, when clicking Send button to send emails to external email domain (not the internal email domain you specified in the VBA code), a warning box will pop up as below screenshot shown.
Related articles:
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!