PDA

View Full Version : Outlook Rules Question



Cheesecube
04-28-2020, 09:53 PM
Hi, I am currently using InStr to look for a String in another String


findRefund = InStr(1, "Refund order - Item code 10000000", "Refund", vbTextCompare)

But what if I want to use multiple search criteria, with capitalization variants?

E.g.


Refund
refund
Exchange
exchange
Return
return

Writing separate InStr lines for each criteria seems inefficient.

How do you search for all of these criteria using minimal code?

SamT
04-29-2020, 12:37 AM
Dim Criteria
Criteria = Array("refund", "exchange", "return") '<--- all lowercase

Dim SearchedString as ??? Range? Array? String? Depends on Data Structure
SearchedString = ???

For i = Lbound(Criteria) To Ubound(Criteria)
Refund = Instr(LCase(SearchedString), Criteria(i))
If Refund then Exit For
Next

Possible usage
For Each Cel in Range("B:B")
If RefundDue(Cel) then cel.Offset(, 1) = "Refund this Item"
Next
End...

Function RefundDue(SearchedString As Variant) As Boolean
Const Criteria = Array("refund", "exchange", "return")

For i = Lbound(Criteria) To Ubound(Criteria)
RefundDue = Instr(LCase(SearchedString), Criteria(i))
If RefundDue then Exit For
Next
End Function

Cheesecube
04-29-2020, 07:32 PM
Outlook Rules Question

Hi, is it possible to specify a rule where Outlook scans all the attachment file names to see if any of them contain any from a list of substrings? Case insensitive.

E.g. any attachment file name contains: refund, Refund, exchange, Exchange, return, Return

I want to avoid doing this via VBA where possible

If not, please provide sample VBA code. Thanks

gmayor
04-30-2020, 02:52 AM
You would need a rule with a script to check the incoming mail items e.g.


Sub CheckAttachments(Item As Outlook.MailItem)
Dim vCriteria As Variant
Dim olAtt As Attachment
Dim i As Integer

vCriteria = Array("refund", "exchange", "return") '<--- all lowercase

If TypeName(Item) = "MailItem" Then
If Item.Attachments.Count > 0 Then
For Each olAtt In Item.Attachments
For i = 0 To UBound(vCriteria)
If InStr(1, LCase(olAtt.fileName), vCriteria(i)) > 0 Then
'do something with olatt
'or do something withy item
End If
Next i
Next olAtt
End If
End If
lbl_Exit:
Exit Sub
End Sub