Solved

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

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

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Get an idea of what you should include in an email disclaimer with these Top 5 email disclaimer tips.
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now