Skip to main content

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

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

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...

Description


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!
Comments (7)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Doesn't work, no pop-up is showing.
This comment was minimized by the moderator on the site
It works just fine, but there a few things you have to change to get it to work every time you load outlook. Macro security settings need to be changed.
This comment was minimized by the moderator on the site
I had the same problem - getting warning also for the internal e-mails.
When the internal e-mail address from Directory is used, xRecipientAddress does not return the address, but some weird exchange string like /O=<organization...> - try to print it in MsgBox and you will see yours.
This comment was minimized by the moderator on the site
Did you overcome this?
This comment was minimized by the moderator on the site
This does not work. I do not get a pop up for internal or external.
This comment was minimized by the moderator on the site
This is prompting both internal and external emails. both domain fields have been updated. Any thoughts? Many thanks
This comment was minimized by the moderator on the site
Hola:



Gran aporte pero me lo pregunta para todos los envíos y no solo para los de una determinada dirección. Es decir, siempre me sale el mensaje independientemente del dominio... ¿está bien el código? Gracias.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations