How to filter item senders from Items_ItemAdd Events?

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
  Dim Ns As Outlook.NameSpace
  Dim Folder As Outlook.MAPIFolder

  Set Ns = Application.GetNamespace("MAPI")
  Set Folder = Ns.GetDefaultFolder(olFolderInbox)
  Set Items = Folder.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
  If TypeOf Item Is Outlook.MailItem Then
    Printattachments Item
  End If
End Sub

      

I made a rule so Outlook will automatically print the incoming email with the attachment except for a few employees.

If I stop the rule, the macro won't work on its own (suppose it must be a code error?), But if the rule triggers every email with an attachment, it will be printed twice.

one with each page, and the other with just the first. Is there a way to get around this? Please help and thanks in advance!

+3


source to share


1 answer


Work with the Items.Restrict Method (Outlook) to exclude the sender's name. Filtering Items

Example

Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
    Dim olNs As Outlook.NameSpace
    Dim Inbox  As Outlook.MAPIFolder
    Dim Filter As String

    Filter = "@SQL=" & " Not (urn:schemas:httpmail:fromname" & _
                       " Like '%Ming Lian%' Or " & _
                             "urn:schemas:httpmail:fromname" & _
                       " Like '%0m3r 0mr%')"


    Set olNs = Application.GetNamespace("MAPI")
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
    Set Items = Inbox.Items.Restrict(Filter)

End Sub

      

Make sure to update %Ming Lian%

with correct name and now you don't need Outlook rule



Items.Restrict method is an alternative to using the Find method or the FindNext method to iterate over specific items in a collection. Find or FindNext is faster than filtering when there are few items. The Constraint method is much faster if the collection has a large number of items, especially if only a few items in a large collection are expected to be found.


Filtering items using string comparison that DASL filters support includes equivalence, prefix, phrase, and substring. Note that when filtering on the Subject property, prefixes such as "RE:" and "FW:" are ignored.

+3


source







All Articles