Search code examples
vbafilteroutlookwebdavadvanced-search

Finding attachments with dav queries using AdvancedSearchMethod


I'm attempting to use the advanced search method to filter through large numbers of emails. I am looking at ultimately creating a filter like:

filter = "(urn:schemas:mailheader:subject like '%project%' or " _ 
& "urn:schemas:httpmail:textdescription like '%project%') and " _
& "urn:schemas:mailheader:date > 'now - 1:00:00' and " _
& "urn:schemas:httpmail:hasattachment > 0 and " _
**& "urn:schemas:httpmail:attachmentfilename like '%tonnage%'"** 
 

The problem is that the part for the attachment's filename (line in bold) does not work. I believe this is because it is looking for something like the olmailitem and not the attachment to that object perhaps. Anyway, I need to get a simple filter that looks at attachments working and then I can scale it up. This is what I have so far:

scope = "'Fakeexample123@outlook.com'"
    
'this is a dumbed down version of my filter,
'but I want it to search for 3 instances of '%attachment%':
    'attachment
    'Attachment
    'ATTACHMENT
    'for any .xls, .xlsm, or .xlsx files
    
    'I was thinking something like:
    'filter = "ucase(urn:schemas:httpmail:attachmentfilename) like '%ATTACHMENT%xls%'"
    'but I don't believe that works... ideas?
    
    filter = "urn:schemas:httpmail:hasattachment > 0 and " _ 
'***************** This is the line that is giving me trouble *****************
    "urn:schemas:httpmail:attachmentfilename like '%attachment%xls%'"
'***************** End trouble *****************
    Set AdvancedSearch = myOlApp.AdvancedSearch(scope, filter, True, "test")

Let me know if you have any thoughts please.


Solution

  • You need to find items with attachments first, for example, you can use the following condition:

      "urn:schemas:httpmail:hasattachment" = 1
    

    Note, a SQL syntax should be used to get it working correctly in Outlook.

      filter = "@SQL=""urn:schemas:httpmail:hasattachment"" = 1"