Skip to main content

Outlook 2007/2010 Search Folders using email address domain

As of May 2010, the Beta of Outlook 2010 still hasn’t overcome this problem. I’m surprised this glaring omission has been left unfixed. Maybe Outlook is maintained by contractors?

I have what I consider to be a simple need. I want a “Search Folder” that shows me all the email related to a particular client. What works well for me is a query that finds any email with the client name in the subject, or any email that involves an email address (from/to/cc) from the client's email domain. Back in Thunderbird, it was simple to setup a rule for this. Outlook can't do it. (I'd go back to Thunderbird, but I have to get calendaring working first.)

What doesn't work

When you edit the criteria for a Search Folder, on the “Messages” tab, the fields you want appear to be represented, but the way things work is wrong. All the criteria specified must be true, not any; they went with “AND” where I need “OR”. The other problem is that the “From…” and “Sent To…” fields use a “starts-with” match, not a “contains” or “ends-with”. So that’s useless.

The next possibility is the “Advanced” tab. Unfortunately, this contains the key mistake that the Outlook development team made. The “From”, “To”, and “Cc” fields use some sort of “display” versions of the data; they often don’t have the address, so they’re also worthless. And the hidden "Query Builder" tab has the exact same problem. The system in Thunderbird is clearly superior---both in terms of usability and functionality. Outlook is confused and crippled.

There is only one option left: the “SQL” tab available when applying a filter. The problems here are these:
  • You have to add a "Filter..." button to the button-bar to get to the dialog box that has the "SQL" tab needed. (You can't get to it from the Search Folder's "Criteria..." dialog.)
  • Documentation for the "SQL" tab is almost non-existent.
  • In the end, it's still not totally possible.
Searching for help

The first place I found help was a blog post by Andrew Delin (Doing more with Outlook filter and SQL DASL syntax) and another by John Wood (Using an SQL Filter in Outlook 2002/2003). The next thing I did was open up the filter dialog and setup criteria on the "Messages" tab, then switch to the "SQL" tab to see what SQL got auto-generated. That actually made things worse for me since the blogs all talked about this "urn:" stuff and Outlook generated "http:" items with hexadecimal numbers.

Subsequent searching turned up these key pages:
Sent Items

During my searching I stumbled upon a significant caveat that hasn't been disclosed anywhere except on this thread here. To quote:
Messages in your own Sent Items folder won't have any Internet headers. You should be able to see that with Outlook Spy or MFCMAPI.exe.
What does that mean? No filters properly work on the "Sent Items" folder when it comes to email addresses (or anything else that depends on header/metadata information); you're stuck with only subject and body. Apparently messages I send aren't "real" emails; the people who designed Outlook think people couldn't possibly want to know about email they've sent to others. I have not been able to find a workaround.

Solution

The only way to remain sane is to concede that the "Sent Items" folder is largely a lost cause. After that, it's "easy". Here's the query I use for a client:

"http://schemas.microsoft.com/mapi/proptag/0x0037001f" LIKE '%clientname%'
OR
"http://schemas.microsoft.com/mapi/proptag/0x0065001f" LIKE '%@emaildomain.com'
OR
"http://schemas.microsoft.com/mapi/proptag/0x0e04001f" LIKE '%@emaildomain.com'
OR
"http://schemas.microsoft.com/mapi/proptag/0x0e03001f" LIKE '%@emaildomain.com'


If this turns up items in "Sent Items", there are 2 reasons: First, it matched the subject. Second, you sent the email via IMAP and the client you used put a copy of the message you sent into "Sent Items" (e.g. Thunderbird in my case).

I used these properties:
  • PR_SUBJECT_UNICODE (0x0037001f)
  • PR_SENT_REPRESENTING_EMAIL_ADDRESS (0x0065001f)
  • PR_DISPLAY_TO_UNICODE (0x0e04001f)
  • PR_DISPLAY_CC_UNICODE (0x0e03001f)
Here are a couple others that looked useful, but I didn't end up using them for one reason or another:
  • PR_SENDER_EMAIL_ADDRESS_UNICODE (0x0c1f001f)
  • PR_SENT_REPRESENTING_EMAIL_ADDRESS_UNICODE (0x0065001f)
  • PR_TRANSPORT_MESSAGE_HEADERS_UNICODE (0x007d001f)

Comments

Unknown said…
Thanks for your help with this.

I used your content plus some vba to create a routine to add a search folders that checks email to/from/cc, subject, message, and a few others for a string. I only needed it for Inbox, but it could be modified.

I have an outlook rule that places a copy of Sent items in my inbox, so I can track them. This search finds those as well.

I don't know how it'll come out in this post, hope it's useful.

-- Doug

Sub CreateSearchFolderForKeyword()
On Error GoTo Err_CreateSearchFolderForKeyword

Dim strFilter As String
strFilter = InputBox(Prompt:="Text to search for:", Title:="Enter Search Text")
If strFilter = "" Then Exit Sub

Dim strDASLFilter As String
strDASLFilter = """urn:schemas:httpmail:fromname"" LIKE '%" & strFilter & "%' " + _
"OR ""urn:schemas:httpmail:textdescription"" LIKE '%" & strFilter & "%' " + _
"OR ""urn:schemas:httpmail:displaycc"" LIKE '%" & strFilter & "%' " + _
"OR ""urn:schemas:httpmail:displayto"" LIKE '%" & strFilter & "%' " + _
"OR ""urn:schemas:httpmail:subject"" LIKE '%" & strFilter & "%' " + _
"OR ""urn:schemas:httpmail:thread-topic"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/received_by_name"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8586001f"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/85a4001f"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/id/{00062041-0000-0000-C000-000000000046}/8904001f"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/proptag/0x0e03001f"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/proptag/0x0e04001f"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/proptag/0x0042001f"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/proptag/0x0044001f"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/proptag/0x0065001f"" LIKE '%" & strFilter & "%' "

Dim strScope As String
strScope = "'Inbox'"

Dim objSearch As Search
Set objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=False, Tag:="SearchFolder")

'Save the search results to a searchfolder and create shortcut to this searchfolder
objSearch.Save (strFilter)

' Clean up.
Set objSearch = Nothing

Exit Sub

Err_CreateSearchFolderForKeyword:

MsgBox "Error # " & Err & " : " & Error(Err)

End Sub

Popular posts from this blog

Live Migration between domains

For those of you like me who aren't experts at all things Active Directory (AD) and Hyper-V Live Migration (LM) permissions, it can be enough of a pain to LM a Virtual Machine (VM) between domains that you simply decide to take the VMs offline to affect the move. See, I only tolerate AD because it's required for LM'ing VMs; there isn't a choice. (It's also required for Windows Clusters, but that's a different topic.) But I figured it out. My back-story is that we setup a cluster using Windows 2012 r1 as the AD Domain Controller (DC) and Hyper-V Server 2012 r1 for the VM hosts. Then we decided we wanted to use r2 for the AD DC and Hyper-V hosts. Upgrading Hyper-V was easy. But I found that there's some unresolved Microsoft bug with Windows Clustering when upgrading the AD DC from Windows 2012 r1 to Windows 2012 r2--- clustering simply doesn't work correctly anymore . So we gave up and created a from-scratch Windows 2012 r2 AD DC then made a new cluster...

SqlBulkCopy and the "colid" error

I thought there was a page explaining this somewhere out there on the Internet, but I can't find it anymore. So here's what I re-discovered. When you try to insert the rows from a DataTable and the data in one of the columns of one of the rows is too big to fit into the destination column in the database, you get a SqlException with this error message: "Received an invalid column length from the bcp client for colid N." (Where "N" is a number.) It doesn't tell you which row, and it's a pain to figure out what column to look at. To determine what column it is referring to, you first need to get a listing of all columns in the table, listed in the order as they have been defined in the database. Next, you remove any columns in the list that are not represented in SqlBulkCopy.ColumnMappings (the order of the column mappings is irrelevant). The list that remains is what "colid" is referring to, with the first column corresponding to colid ...