Consulting

Results 1 to 4 of 4

Thread: Outlook Rules Question

  1. #1

    Lightbulb Outlook Rules Question

    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?
    Last edited by Cheesecube; 04-28-2020 at 10:16 PM. Reason: Changed question to how to search for multiple criteria instead

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,812
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    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

  4. #4
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •