none
Apostrophes contractions and escape char issues with Outlook DASL Queries in C# VSTO for Outlook RRS feed

  • Question

  • I have anoutlook plug-in that I wrote in C# it scans emails, and shuffles them around to different folders. Because the terms we search for change frequently enough that adding them as variables in the program would be totally unacceptable; on top of that the lists are quite long! I made it so the code slurps up rows from a CSV and uses that term to in the DASL query.

    My problem is, when we need to search for words like "don't", "haven't", "shan't", "Dave's" and other contractions or possessive nouns cause issues. I have tried everything I can think of to ameliorate the issue, I am unfortunately at a loss. The program works flawlessly unless a contraction is introduce. Please help :)

    The most relevant chunk of code is this:

             using (StreamReader r = new StreamReader(fileLoc))
            {
                //while != null to kill the loop
                string line;
                string prefix = @"@SQL=urn:schemas:httpmail:textdescription like '"; 
                string postfix = @"'";
                while ((line = r.ReadLine()) != null)
                {
                    //slurp the csv line out, and spit it into DASL query.
                    line = prefix + line + postfix;
                    lines.Add(line);
    
                }
            } //closes out the streamreader object?
            string[] outputArray = lines.ToArray();
            return outputArray;

    The module I wrote that searches the email is this:

      public void SearchEmailBodyWithArray(string[] filter)
            {
                //this is the workhorse of this program
                MessageBox.Show("Pick Source Folder");
                Outlook.MAPIFolder olFolder = Application.Session.PickFolder();
                MessageBox.Show("Pick Destination Folder");
                Outlook.MAPIFolder destFolder = Application.Session.PickFolder();
                Outlook.Items items = olFolder.Items;
                Outlook.MailItem mailItem = null;
    
                object folderItem;
                string subjectName = string.Empty;
    
                int f = 0;
                while ( f < filter.Length)
                {
                    folderItem = items.Find(filter[f]);
                    while (folderItem != null)
                    {
                        mailItem = folderItem as Outlook.MailItem;
                        if (mailItem != null)
                        {
                            subjectName += "\n" + mailItem.Subject;
                            mailItem.Move(destFolder);
    
    
                        }
                        folderItem = items.FindNext();
                    }
                    f++;
                }
                subjectName = " The following e-mail messages were found: " +
                    subjectName;
                MessageBox.Show(subjectName);
    
            }

    The CSV file I use is one column of terms that looks something like this.

    %i do not%
    %i flipped it%
    %no thanks%

    When I try to add something like %don't do that% or %won't%, I get problems.

    I've tried things like %don''t%, %"don't"%, %don/'t%, %dochr(39)t% sadly, all were unsuccessful.

    The closest I've come is getting some luck by removing the surrounding % signs % and using a double apostrophe but then it sort of just crashes.

    How do I make the DASL query take an apostrophe? And, how do I make it accept that apostrophe when it exists in a record in a CSV file?

    Thanks :)

    Thursday, June 28, 2018 5:45 PM

Answers

  • Hello Dave Babler,

    In our test,

    %don''t% worked on "We don't do that".

    %don’t% worked on "We don’t do that".

    If you are not sure the what it is in your mail, use both of them to filter your mail.

    Best Regard,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Dave Babler Wednesday, July 25, 2018 5:49 PM
    Friday, July 6, 2018 8:27 AM

All replies

  • Hello Dave,

    Try to use a back slash symbol for including apostrophes. For example, don\'t.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, June 28, 2018 7:38 PM
  • Hello Dave Baler,

    According to the simple string in Items.Find, I think %don''t% should be a correct format.

    filter = "@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0037001f"" = 'can''t'"

    And in my test, it does work for me too.

    I'm wondering if you mix up ['] and [’] .

    If your message text is don’t, you could use %don’t% directly.

    If your message text is don't, you need use %don''t%.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, June 29, 2018 9:26 AM
  • I tried that and I'm afraid it failed. :(

    Monday, July 2, 2018 6:40 PM
  • So I tried adding haven't into my email box and sending myself an email and testing it.  It got all of the other words in my CSV file EXCEPT for haven't. :(

    I tried adding the contractions like this.

    %have moved out of state%
    %I now live in%
    %I live in%
    %no longer live in%
    %no longer living%
    %has moved to%
    %have moved to%
    %no longer in%
    %no longer live in%
    %moved to%
    %moved%
    %no longer reside%
    %haven''t%
    %how''s%
    %can''t%
    • I also tried as below:


    %have moved out of state%
    %I now live in%
    %I live in%
    %no longer live in%
    %no longer living%
    %has moved to%
    %have moved to%
    %no longer in%
    %no longer live in%
    %moved to%
    %moved%
    %no longer reside%
    %haven't%
    %how's%
    %can't%
    

    I sent myself 3 test emails the subjects were all "test" and the bodies were as follows:

    • I haven't lived there in years
    • I can't possibly fathom this
    • How's it going

    It found none of these.

    Are you suggesting that I change my code to remove the LIKE from it?

    Also with regards to 

    Your statement "

    If your message text is don’t, you could use %don’t% directly.

    If your message text is don't, you need use %don''t%."

    The first part of both of those sentences is exactly the same, so I'm not sure what you mean. :(

     Can you please clarify. 

    Monday, July 2, 2018 6:53 PM
  • Hello Dave Babler,

    Hope below screenshots could explain what I mean more clear.

    Besides, what's the version of your Office? Could you reproduce the issue on another PC Outlook Client? 

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 3, 2018 2:00 AM
  • I see. 

    I have tried using both of those versions to no avail.  I'm using Outlook Version 1780 Build 8431.2270 of Office 365.

    I receive the error both at home and at work.  

    However!  Your suggestion has helped me narrow down specifically what is going on.

    I sent some test messages saying this like "We don't do that" and "We don’t" do that.  Based on my testing I now understand where the inconsistency is happening I just don't know why or how to fix it.

    The search term 

    %don''t% worked on "We don't do that".

    Unfortunately for some reason we get emails that come from users that use different versions of the apostrophe 

    So when I tested 

    %don’t%

    and 

    %don’’t%

    I even tried %don'’t%, %don\’t%

    It did not find the phrase. I'm not sure why. 

    In fact using %don'’t% in the list caused Outlook to crash but I can't seem to get any exception info out of it. :( Removing it resolved the issue but still doesn't find "don’t".

    So I'm not sure why is being such a pain, but this is for sure, driving me crazy.

    Thanks for all your help so far.  

    Thursday, July 5, 2018 4:17 PM
  • Hello Dave Babler,

    In our test,

    %don''t% worked on "We don't do that".

    %don’t% worked on "We don’t do that".

    If you are not sure the what it is in your mail, use both of them to filter your mail.

    Best Regard,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Dave Babler Wednesday, July 25, 2018 5:49 PM
    Friday, July 6, 2018 8:27 AM
  • I'm still having trouble with the curly style apostrophes but this is close enough so I'm marking it as answered.  Thank you for your assistance!

    Dave 

    Wednesday, July 25, 2018 5:49 PM