Solved

Get email address from emails in the "To" section of an outgoing email

Posted on 2016-10-30
7
54 Views
Last Modified: 2016-10-30
I am using the following VBA code to automatically attach a PDF file when an email is sent out either from a new email from scratch, a reply, or a forwarded email. It first prompts the user if they want to attach the PDF file or not. I need to add something to this code that says IF the email address being sent to is part of our companies domain then bypass the question and DO NOT attach the PDF file. If the email address is NOT part of our companies domain then continue with the code as it's written out. Which means it will ask the question and let the user decide if they want to attach the PDF file or not. I just don't want the popup question to appear when they are sending emails to users at our company. Nor do I want the PDF file to be attached when sending emails internally. How can I add this to my code?

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim MyAttachments As Outlook.Attachments
Dim Prompt As String
Set MyAttachments = Item.Attachments
Res = Dir("Y:\Sales\Sales-Ops\Outlook\Terms&Conditions\BC_Terms_and_Conditions.pdf")

Prompt = "Do you want to attach the BC Terms & Conditions to this email " & Item.Subject & "?"
 
If MsgBox(Prompt, vbYesNo + vbQuestion, "BC Terms & Conditions") = vbYes Then
    If Res <> "" Then
        'Attach File
        MyAttachments.Add Source:="Y:\Sales\Sales-Ops\Outlook\Terms&Conditions\BC_Terms_and_Conditions.pdf", Type:=olByValue, DisplayName:="Disclaimer"
    End If
End If
End Sub

Open in new window

0
Comment
Question by:Lawrence Salvucci
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 2

Expert Comment

by:Jimy
ID: 41866196
Hi Lawrence

as I understand you need validaton on your domain, so you can search domain in "To" and skip attachment stuff. As I don't have outlook here I can't test this but you can try this:

Add this after: Set MyAttachments = Item.Attachments


Set MyDomain = "@MYDOMAIN.COM"
Dim Rcpnt As Recipient

For Each Rcpnt In Item.Recipients
 If InStr(1, UCase(Rcpnt.AddressEntry), MyDomain, vbTextCompare) Then
   Exit Sub
 End If
Next

Regards,
Jimy
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866208
Yes correct. If the "to" line has anyone with our domain in their email address then just bypass all the code. I only want that code to fire for people outside our domain.

I am getting a compile error: object required. It highlights "MyDomain: in the set MyDomain line. I added "Dim MyDomain As String" but it still gives me that error
0
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 250 total points
ID: 41866212
Hi,

If not all the recipients  are in your firm the code before won't work
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim MyAttachments As Outlook.Attachments
Dim Prompt As String
Dim MyDomain As String
Dim recip As Outlook.Recipient 
Dim pa As Outlook.PropertyAccessor 
Const PR_SMTP_ADDRESS As String = _ 
        "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" 
MyDomain = "@mydomain.com"
bExternalMail = False
For Each recip In Item.Recipients
    Set pa = recip.PropertyAccessor
    If Not LCase(pa.GetProperty(PR_SMTP_ADDRESS) ) Like "*" & MyDomain "*" Then
        bExternalMail = True
    End If
Next
If bExternalMail = False Then Exit Sub

Set MyAttachments = Item.Attachments
Res = Dir("Y:\Sales\Sales-Ops\Outlook\Terms&Conditions\BC_Terms_and_Conditions.pdf")

Prompt = "Do you want to attach the BC Terms & Conditions to this email " & Item.Subject & "?"
 
If MsgBox(Prompt, vbYesNo + vbQuestion, "BC Terms & Conditions") = vbYes Then
    If Res <> "" Then
        'Attach File
        MyAttachments.Add Source:="Y:\Sales\Sales-Ops\Outlook\Terms&Conditions\BC_Terms_and_Conditions.pdf", Type:=olByValue, DisplayName:="Disclaimer"
    End If
End If
End Sub

Open in new window

Regards
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866217
I am getting a syntax error on this line:

    If Not LCase(pa.GetProperty(PR_SMTP_ADDRESS) ) Like "*" & MyDomain "*" Then

Open in new window

0
 
LVL 2

Assisted Solution

by:Jimy
Jimy earned 250 total points
ID: 41866226
Add &
    If Not LCase(pa.GetProperty(PR_SMTP_ADDRESS) ) Like "*" & MyDomain & "*" Then

Open in new window


Regards,
Jimy
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866227
Works like a charm! Exactly what I wanted! Thank you very much!
0
 
LVL 2

Expert Comment

by:Jimy
ID: 41866230
Glad to hear that.  Thank you for points and also to Rgonzo1971, as we learnt something new.
Jimy
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question