Solved

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

Posted on 2016-10-30
7
51 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
Independent Software Vendors: 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

SendBlaster Pro 4 - Bulk Email Sending Software

SendBlaster 4 Pro - Best Bulk Emailing Sending Software
Automatic Subscribe / Unsubscribe Processing
Great for Newsletters & Mass Mailings
Optional HTML & Text Composition
Integration with Google Features
Built in Spam Score Checking
Free Professional Templates - Feature Packed!

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…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

737 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