Filter on SendItems Folder for past emails Sent to a user

Add-in Express™ Support Service
That's what is more important than anything else

Filter on SendItems Folder for past emails Sent to a user
I need a way to check if I have had any email exchanges with a particular email id  
TheNewCOMAddin Dev




Posts: 19
Joined: 2019-07-11
Problem Statement
Note: This not be ADX Issue, but Generic.
I am looking for a way to count of any email was sent to a particular email, the logic being that it was Sent it would be in the "Sent Items".

Issue
I am using the below code which I believe is close to being correct but is not working i.e I don't get a count. Probably I am missing out on something. If anyone has a solution would like to hear how to best do this.

Earlier Attempts
1. Items.Find("[To] = 'search@gmail.com'")
2. Items.Restrict("[To] = 'search@gmail.com'")
None of them gave me results.

The How


        public int GetSentEmailExchangeCount(string SenderEmailId)
        {
            Outlook.MAPIFolder sentItems = (Outlook.MAPIFolder)this.OutlookApp.ActiveExplorer().Session.GetDefaultFolder((Outlook.OlDefaultFolders.olFolderSentMail));
            Outlook.Items folderItems = (Outlook.Items)sentItems.Items;
            string searchCriteria = "@SQL="urn:schemas:httpmail:displayto" LIKE '%" + SenderEmailId + "%'";
            StringBuilder strBuilder = null;
            int counter = default(int);
            Outlook._MailItem mail = null;
            object resultItem = null;
            try
            {
                strBuilder = new StringBuilder();                
                resultItem = folderItems.Restrict(searchCriteria);
                while (resultItem != null)
                {
                    if (resultItem is Outlook._MailItem)
                    {
                        counter++;
                    }
                    Marshal.ReleaseComObject(resultItem);
                    resultItem = folderItems.FindNext();
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            finally
            {
                if (folderItems != null) Marshal.ReleaseComObject(folderItems);
            }        

            return counter;
        }
Posted 05 Dec, 2019 09:11:51 Top
Andrei Smolin


Add-in Express team


Posts: 16814
Joined: 2006-05-11
Hello ,

resultItem = folderItems.Restrict(searchCriteria);

Items.Restrict() returns Outlook.Items, not an Outlook item; see https://docs.microsoft.com/en-us/office/vba/api/outlook.items.restrict.

TheNewCOMAddin Dev writes:
None of them gave me results.


Navigate to that folder and click View Settings (on the View tab). Specify a filter, close the dialog and make sure the filter works as expected. then open this dialog once again and switch to the SQL tab to see the actual syntax to be used in your code.


Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 05 Dec, 2019 09:34:08 Top