Lawrence Salvucci
asked on
Get email address from emails in the "To" section of an outgoing email
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
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am getting a syntax error on this line:
If Not LCase(pa.GetProperty(PR_SMTP_ADDRESS) ) Like "*" & MyDomain "*" Then
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works like a charm! Exactly what I wanted! Thank you very much!
Glad to hear that. Thank you for points and also to Rgonzo1971, as we learnt something new.
Jimy
Jimy
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),
Exit Sub
End If
Next
Regards,
Jimy