VBA: Find whether there is common pattern between the email subject and attachment name - vba

I want to validate that the outgoing email is correctly attached with a correct file. The email subject contains a code. The attachment filename is automatically generated with a code and attached manually to the email. The VBA is to check whether the email subject contains a common pattern in the filename of the attachment.
The code is like H??#######, i.e. it must start with "H", followed with 2 letters, and then 7 digits.
If both the email subject and filename contain the same code, the email is allowed to send, otherwise it should warn. For example:
Subject: Urgent Chapter 10 - HCX1234567 updated on 12 Dec 2015
Filename: HCX1234567_ABCCh10_20151212_0408
This email is allowed.
Is it possible to do such validation before sending?
Here is my attempt:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Create Geoff Lai on 14 March 2016
Dim recips As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim pa As Outlook.PropertyAccessor
Dim prompt As String
Dim strMsg As String
Dim mailContent As String
Dim jobCode As String
Dim attachName As String
Dim pos As Integer
Dim jcodepos As Integer
Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
attachName = Item.Attachments.Item(1).FileName
mailContent = Item.Body + Item.Subject ' Get a copy of all the e-mail body text and subject text to search.
mailContent = LCase(mailContent) ' Make whole string lowercase for easier searching.
Set recips = Item.Recipients
For Each recip In recips 'Record email addressees if send to external domain
Set pa = recip.PropertyAccessor
If InStr(LCase(pa.GetProperty(PR_SMTP_ADDRESS)), "#mydomain.com") = 0 Then
strMsg = strMsg & " " & pa.GetProperty(PR_SMTP_ADDRESS) & vbNewLine
End If
Next
If strMsg <> "" Then
If (Item.Attachments.Count = 0) Then ' Check attachment
If InStr(1, mailContent, "attach") > 0 Then
pos = 1
ElseIf InStr(1, mailContent, "Attach") > 0 Then
pos = 1
ElseIf InStr(1, mailContent, "enclose") > 0 Then
pos = 1
ElseIf InStr(1, mailContent, "Enclose") > 0 Then
pos = 1
Else: pos = 0
End If
End If
If (pos > 0) Then 'If there is no attachment:
If MsgBox("With the word attach or enclose, attachment should be found in this email" & vbNewLine & "Please Confirm.", vbYesNo + vbCritical + vbMsgBoxSetForeground, "Missing Attachment") = vbYes Then
prompt = "This email will be sent outside of mydomain.com to:" & vbNewLine & strMsg & "Do you want to proceed?"
If MsgBox(prompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
Exit Sub
Else
Exit Sub
End If
Else
Cancel = True 'Stop sending
End If
End If
If (Item.Attachments.Count > 0) Then ' Validate attachment and subject
jcodepos = InStr(1, attachName, "H??#######", 0) ' Get job code position
jobCode = Mid(attachName, jcodepos, 10) ' Get job code
If (InStr(1, Item.Subject, jobCode, 0) = 0) Then ' If no common code between subject and attachment
If MsgBox("There is no common job code between the email subject and the filename of the attachment." & vbNewLine & "Do you want to proceed?", _
vbYesNo + vbCritical + vbMsgBoxSetForeground, "Wrong Attachment?") = vbNo Then
Cancel = True
Exit Sub
Else
Exit Sub
End If
ElseIf MsgBox("Common job code " & jobCode & " is found in the email subject and the filename of the attachment" & prompt, _
vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Confirm Job Code") = vbNo Then ' If common code is found
Cancel = True
Exit Sub
Else
Exit Sub
End If
End If
End If
End Sub
However, I get an error at jobCode = Mid(attachName, jcodepos, 10), which is:
Run-time error '5' Invalid procedure call or argument

Application_ItemSend, the usual way, in ThisOutlookModule. How can I automatically run a macro when an email is sent in Outlook?
In the VB editor set the reference to Regular Expressions.
Similar to the code in the Question part of Regular Expression Rules in Outlook 2007?. Check RegEx.Pattern = "(H[A-Z]{2}[0-9]{7})" against the filename. Continue with RegEx or InStr to verify the subject includes the filename match.

Since you thinking about using VBA I would assume that you are using Outlook as your email client. If so, please add this to your question and the tags. With this assumption the answer is that it depends:
If Outlook is actually used to send the email then it can be done. The following Q&A is probably a good starting point.
how to check details before sending mails in outlook using macros?
Yet, the above technique will not work if the email is created with File | Send commands in Office programs or similar commands in Windows Explorer or other programs.

Finally, I have figured it out, Thanks for the advices!
Here is my workout.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim recips As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim pa As Outlook.PropertyAccessor
Dim regex As Object, codeInSubject As Object, codeInAttach As Object
Dim matchSbjtCode As String, matchAttchcode As String
Dim prompt As String
Dim strMsg As String
Dim mailContent As String
Dim attachName As String
Dim pos As Integer
Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Set regex = CreateObject("vbScript.regExp")
With regex
.Pattern = "[H][ACDILNOPQTUVW][BCGJMOPRSTWY][1-9][0-9]{6}" ' Set regular expression pattern
.Global = False ' Check the first instance only
End With
attachName = Item.Attachments.Item(1).FileName
mailContent = Item.Body + Item.Subject ' Get a copy of all the e-mail body text and subject text to search.
mailContent = LCase(mailContent) ' Make whole string lowercase for easier searching.
Set recips = Item.Recipients
For Each recip In recips 'Record email addressees if send to external domain
Set pa = recip.PropertyAccessor
If InStr(LCase(pa.GetProperty(PR_SMTP_ADDRESS)), "#mydomain.com") = 0 Then
strMsg = strMsg & " " & pa.GetProperty(PR_SMTP_ADDRESS) & vbNewLine
End If
Next
prompt = "This email will be sent outside of mydomain.com to:" & vbNewLine & strMsg & "Do you want to proceed?"
If strMsg <> "" Then
If (Item.Attachments.Count = 0) Then ' Check attachment
If InStr(1, mailContent, "attach") > 0 Then
pos = 1
ElseIf InStr(1, mailContent, "enclose") > 0 Then
Else: pos = 0
End If
End If
If (pos > 0) Then 'If there is no attachment:
If MsgBox("With the word 'attach' or 'enclose', attachment should be found in this email" & vbNewLine & _
"Please Confirm.", vbYesNo + vbCritical + vbMsgBoxSetForeground, "Missing Attachment") = vbYes Then ' Prompt to check
If MsgBox(prompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
Exit Sub
Else
Exit Sub
End If
Else
Cancel = True 'Stop sending
End If
End If
If (Item.Attachments.Count > 0) Then ' Validate attachment and subject
If regex.test(Item.Subject) And regex.test(attachName) Then ' Test the job codes in the email subject and attachment filename
Set codeInSubject = regex.Execute(Item.Subject)
Set codeInAttach = regex.Execute(attachName)
If StrComp(codeInSubject(0), codeInAttach(0)) = 0 Then ' Compare the codes found
If MsgBox("Common job code """ & codeInAttach(0) & """ is found in the email subject and the filename of the attachment. " & vbNewLine & prompt, _
vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Confirm Job Code") = vbNo Then ' If found, confirm to send
Cancel = True
Else: Exit Sub
End If
ElseIf MsgBox("There is no common job code between the email subject and the filename of the attachment." & vbNewLine & _
"Do you want to DISCARD?", vbYesNo + vbCritical + vbMsgBoxSetForeground, "Wrong Attachment?") = vbYes Then ' if not found, discard
Cancel = True
Else: Exit Sub
End If
End If
End If
End If
End Sub

Related

VBA code to create a task when sending emails

I would like to be able to create a task from a Sent email in Outlook at the time of sending. It is find if a button pops up asking me if I want a task, and I have to click Yes or No. I found the code below. But if I click Yes, it creates the task without me editing it. I just want the Task window to pop up with the email I sent populating it, but I want to add the dates etc. Can anyone help please?
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Updated by Extendoffice 20181123
Dim xYesNo As Integer
Dim xPrompt As String
Dim xTaskItem As TaskItem
Dim xRecipient As String
On Error Resume Next
xPrompt = "Do you want to create a task for this message?"
xYesNo = MsgBox(xPrompt, vbYesNo + vbInformation, "Kutools for Outlook")
Cancel = False
If xYesNo = vbNo Then Exit Sub
Set xTaskItem = Application.CreateItem(olTaskItem)
For Each Rcp In Item.Recipients
If xRecipient = "" Then
xRecipient = Rcp.Address
Else
xRecipient = xRecipient & vbCrLf & Rcp.Address
End If
Next Rcp
xRecipient = xRecipient & vbCrLf & Item.Body
With xTaskItem
.Subject = Item.Subject
.StartDate = Item.ReceivedTime
.DueDate = Date + 3 + CDate("9:00:00 AM")
.ReminderSet = True
.ReminderTime = Date + 2 + CDate("9:00:00 AM")
.Body = xRecipient
.Save
End With
Set xTaskItem = Nothing
End Sub
You need to call the Display method if you need to show an Outlook inspector window for editing task properties:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Updated by Extendoffice 20181123
Dim xYesNo As Integer
Dim xPrompt As String
Dim xTaskItem As TaskItem
Dim xRecipient As String
On Error Resume Next
xPrompt = "Do you want to create a task for this message?"
xYesNo = MsgBox(xPrompt, vbYesNo + vbInformation, "Kutools for Outlook")
Cancel = False
If xYesNo = vbNo Then Exit Sub
Set xTaskItem = Application.CreateItem(olTaskItem)
For Each Rcp In Item.Recipients
If xRecipient = "" Then
xRecipient = Rcp.Address
Else
xRecipient = xRecipient & vbCrLf & Rcp.Address
End If
Next Rcp
xRecipient = xRecipient & vbCrLf & Item.Body
With xTaskItem
.Subject = Item.Subject
.StartDate = Item.ReceivedTime
.DueDate = Date + 3 + CDate("9:00:00 AM")
.ReminderSet = True
.ReminderTime = Date + 2 + CDate("9:00:00 AM")
.Body = xRecipient
.Display
End With
Set xTaskItem = Nothing
End Sub
In that case the original window should be closed and the email is sent out. But you will get the task item window opened for editing its properties. If you need to delay the send process until you are done with task properties editing process you may pass true to the Display method to stop the execution of the ItemSend event handler and wait until the dialog window is closed.

Outlook Macro to match domain name from a list in excel

I need a macro which can match domain name of the email ids in TO and CC from a list of emails(preferably from excel) and if any of the email address does not match, it should throw a pop-up asking if the user wants to continue and if yes then the mail should be sent as it is and a email id should be added in BCC.
Please find the sample code, it works but I also want to compare the domain name as a sub-string in the subject.
Ex: The if the subject line is "ABC Report- Company1- Jan-2 and it is sent to a1#company1.com, a2#compay2.com then it should prompt that the a2#company2.com is an unauthorized email and ask if still the user want to proceed, if Yes it should copy admin#mycompany.com in BCC and delay the mail by 5mins.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim recips As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim pa As Outlook.PropertyAccessor
Dim prompt As String
Dim strMsg As String
Dim Address As String
Dim lLen
Dim strSubject As String
Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
strSubject = Item.Subject
If strSubject Like "*ACB Report*" Or strSubject Like "*XYZ Report*" Then
   
Set recips = Item.Recipients
For Each recip In recips
Set pa = recip.PropertyAccessor
 Address = LCase(pa.GetProperty(PR_SMTP_ADDRESS))
lLen = Len(Address) - InStrRev(Address, "#")
Select Case Right(Address, lLen)
    Case "cdolive.com", "gmail.com", "slipstick.com", "outlookmvp.com"
        
    Case Else ' remove case else line to be warned when sending to the addresses
     strMsg = strMsg & " " & Address & vbNewLine
End Select
Next
If strMsg <> "" Then
prompt = "This email will be sent outside of the company to:" & vbNewLine & strMsg & vbNewLine & "Please check recipient address." & vbNewLine & vbNewLine & "Do you still wish to send?"
If MsgBox(prompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
End If
End If
End If
End Sub
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim recips As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim pa As Outlook.PropertyAccessor
Dim prompt As String
Dim strMsg As String
Dim Address As String
Dim lLen
Dim strSubject As String
Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
strSubject = Item.subject
If strSubject Like "*ABC Report*" Or strSubject Like "*XYZ Report*" Then
Set recips = Item.Recipients
For Each recip In recips
If recip.Type <> olBCC Then
Set pa = recip.PropertyAccessor
Address = LCase(pa.GetProperty(PR_SMTP_ADDRESS))
'rlen = Len(Address) - InStrRev(Address, "#")
'If strSubject Like "*rlen*" Then
lLen = Len(Address) - InStrRev(Address, "#")
'Select Case Left(Address, rlen)
'Case "acceture", "slipstick"
'Case Else
'strMsg = strMsg & " " & Address & vbNewLine
'End Select
'Next
Dim SendMail As Boolean
Select Case Right(Address, lLen)
Case "cdolive.com", "slipstick.com", "outlookmvp.com", "accenture.com"
' "select case" is doing nothing in this case
SendMail = True
Case Else ' remove case else line to be warned when sending to the addresses
strMsg = strMsg & " " & Address & vbNewLine
End Select
If strMsg <> "" And Not SubjectContainsEmailDomain(strSubject, Address) Then
prompt = "The system has detected that you are sending this email to some unauthorized user:" & vbNewLine & strMsg & vbNewLine & "Please check recipient address." & vbNewLine & vbNewLine & "Do you still wish to send?"
If MsgBox(prompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
Else
' add BCC
Dim objRecip As Recipient
Set objRecip = Item.Recipients.Add("myid#gmail.com")
objRecip.Type = olBCC
objRecip.Resolve
'MailItem.DeferredDeliveryTime = DateAdd("n", 90, Now)
End If
End If
' Cancel if not in "cdolive.com", "slipstick.com", "outlookmvp.com"
If Not SendMail Then Cancel = True
MsgBox "The entered email address(s) are not aliged to you" & vbNewLine & "Please add the domain name in the code"
'End If
'End If
End If
Next
Last:
End If
End If
End If
End Sub
Function GetDomain(emailAddress As String) As String
Dim arr As Variant
arr = Split(emailAddress, "#")
GetDomain = Left(arr(1), InStrRev(arr(1), ".") - 1)
End Function
Function SubjectContainsEmailDomain(subject As String, email As String) As Boolean
Dim domain As String
domain = GetDomain(email)
Dim index As Integer
SubjectContainsEmailDomain = InStr(LCase(subject), LCase(domain))
End Function
The next to last part of an email address is the Second Level Domain (2LD).
This seems to be finding Recipient2LD that is different from the Subject Company.
The Subject seems to be free form typing by users, and I have no idea how to parse the SubjectCompany out of the Subject line, but if you could then this could be added after EndSelect and before Next.
Dim RecipDomainParts() As String
RecipDomainParts = Split(Right(Address, lLen), ".")
Dim Recip2LD As String ' Recipient Second Level Domain
Recip2LD = DomainParts(UBound(DomainParts) - 1)
' I have no idea how to parse the SubjectCompany out of the Subject line
If Recip2LD <> SubjectCompany Then
strMsg = strMsg & " " & Address & vbNewLine
End If
->>added 9/2/18
you need to decide yourself the general outline of your process: whether to possibly have an error message for each Recipient for each problem (List or Subject) or to combine into one message for a Recipient, while doing each Recipient, or append each msg into one message at the end of all Recipients... Then follow your outline. Work at refining the outline first, then write the code to match.
It may be good to make sub for "Recip_in_List" and make a sub for "RecipDomain_in_Subject" after you revise the outline.
BCC probably should not be skipped, as user might try to put an email there.
Your xyz#qwerty.com should be in the List.
variable SendMail cannot be set to True because it would wipe out False that had been set on prior Recipient. By doing Exit Sub when vbNo you eliminate this boolean.
Set Delay = 0min
For each Recip
If Recip not in List
Popup to user
If vbNo then Cancel=True and exit without send
Else add BCC of xyz#qwerty.com if not there
endif
endif
If RecipDomain not in Subject
Popup to user
If vbNo then Cancel=True and exit without send
Else add BCC of admin#qwerty.com if not there
set Delay = 5min
endif
endif
Next Recip
SEND with Delay

VBA code not running on Outlook restart

When I close and relaunch Outlook the macro doesn't work.
This code will validate before sending the mail in Outlook for subject and attachments.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim lngAns As Long
Dim varArray As Variant
Dim strWordFound As String
varArray = Array("PFA", "Attached", "Enclosed", "File", "Report")
For lngCount = LBound(varArray) To UBound(varArray)
If InStr(1, Item.Body, varArray(lngCount), vbTextCompare) Or InStr(1, Item.Subject, varArray(lngCount), vbTextCompare) Then
strWordFound = strWordFound & "," & varArray(lngCount)
End If
Next
strWordFound = Mid(strWordFound, 2)
If Len(strWordFound) > 0 And Item.Attachments.Count = 0 Then
If MsgBox("Found No Attachments but the Word(s): " & _
strWordFound & vbTab & vbCr & "Do you want to send the mail anyway?", _
vbYesNo + vbQuestion, "Attachment Missing") = vbNo Then Cancel = True
End If
If Len(Trim(Item.Subject)) = 0 Then
If MsgBox("Subject is Empty. Are you sure you want to send the Mail?", _
vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Subject Missing") = vbNo Then Cancel = True
End If
End Sub
Update Macro Security settings in the Trust Center.
Some links:
Enable or disable macros in Office documents
Outlook 2013 & 2010: Enable or Disable Macros

Outlook VBA to verify recipient

What is the best way to check for multiple black-list email addresses before sending?
I have several email addresses I am not allowed to send information to as part of a project. I want Outlook to check for any of the black-list email addresses and notify me if they are included before sending. Below is the code I found to modify
For example, my black-list includes: "bad#address.com", "worst#address.com", "evil#address.com"
What is the best way to put these addresses into the code below, would be good for it to be in a way that allows for changing the addresses in the black-list easily?
So here is the latest version of my code with your suggestions. Unfortunately it lets me send the emails to the addresses on the Checklist. Any suggestions?
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim Recipients As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim i
Dim prompt As String
On Error Resume Next
' use lower case for the address
' LCase converts all addresses in the To field to lower case
Checklist = "bad#address.com" & _
"worst#address.com" & _
"evil#address.com" '// , _ and so on
Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1
Set recip = Recipients.Item(i)
If InStr(LCase(recip), LCase(Checklist)) Then
prompt$ = "You sending this to this to " & Item.To & ". Are you sure you want to send it?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
End If
End If
Next i
End Sub
Create a procedure level variable CheckList which is the list of csv of black listed emails. you can initialise this in the procedure as a hard assignment or dynamically retrieve from other data sources for e.g. sql server
Dim lbadFound As Boolean
dim badAddresses as string
lbadFound = False
CheckList = "bad#address.com," & _
"worst#address.com," & _
"evil#address.com" '// , _ and so on
Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1
Set recip = Recipients.Item(i)
If instr(1,lcase(CheckList), LCase(recip)) >=1 Then
lbadFound = true
badAddresses = badAddresses & recip & & vbcrlf
End If
Next i
If lbadFound Then
prompt$ = "You sending this mail to one or more black listed email address(es)" & badAddresses & vbcrlf & " Are you sure you want to send it?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
End If
End If

VBA Outlook Script

I have a script I've pieced together over time. Recently a plugin we need to use in Outlook has caused some issues. Basically we get prompted twice because when the plugin is used the e-mail ends up being destroyed and re-created with a specific attachment filename. At this point the user is prompted again. I've tried to work in a For loop to skip the script if it finds this attachment. However, when I added the For loop it just seems to skip the entire script. I have limited experience with VBA and so I'm sure it's an issue with my syntax or usage. See script below:
Private Sub Application_ItemSend _
(ByVal Item As Object, Cancel As Boolean)
Dim strMsg As String
Dim Atmt As Variant
'strMsg = Item.Class
If Item.Class = "43" Then
For Each Atmt In Item.Attachments
If VBA.Right(Atmt.FileName, 3) = ".sf" Then
GoTo NonEmailError
End If
Next Atmt
If Item.CC = "" Then
strMsg = "To recipients: " & Item.To & vbCrLf & _
"Are you sure you want to send this message?"
Else
strMsg = "To recipients: " & Item.To & vbCrLf & _
"Cc recipients: " & Item.CC & vbCrLf & _
"Bcc recipients: " & Item.BCC & vbCrLf & _
"Are you sure you want to send this message?"
End If
Else
GoTo NonEmailError
End If
' Exit Sub
' Ignore errors for now.
On Error GoTo NonEmailError
' Prompt user to fill in subject
If Item.Subject = "" Then
MsgBox "You must enter a subject.", 48, "Empty Subject"
Cancel = True
GoTo NonEmailError
End If
' Exit Sub
' Prompt user to verify E-Mails
If MsgBox(strMsg, vbYesNo + vbQuestion _
, "Send Confirmation") = vbNo Then
Cancel = True
End If
Exit Sub
NonEmailError:
' The item being sent was not an e-mail and so don't prompt the user anything
Exit Sub
End Sub
I am not that familiar with outlook and VBA, but my first guess in this case is that item.class should be compared to the numeric value 43 rather than the string-literal value "43".
The one thing that gives me pause is that I would expect the statement to throw an error because "43" is the wrong type for Item.Class, but this may just reflect my unfamiliarity with the material.
According to msdn reference, item.class (in this case a mailItem) is an OlObjectClass constant, and 43 is olMail. It would probably be advisable to change:
If Item.Class = "43" Then
to
If Item.Class = olMail Then
or at least to:
If Item.Class = 43 Then
( see: http://msdn.microsoft.com/en-us/library/bb208118%28v=office.12%29.aspx and http://msdn.microsoft.com/en-us/library/bb207137%28v=office.12%29.aspx )