Solved

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

Posted on 2016-10-30
7
39 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 49

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

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.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this step by step procedure, you will come to know the details of creating an Outlook meeting in 2007, 2010, 2013 & 2016.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

770 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