Solved

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

Posted on 2016-10-30
7
48 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
  • 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 50

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

685 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