none
How to save results of AdvancedSearch multiple times? RRS feed

  • Question

  • Hi,

    I am developing Outlook add-in with VSTO. I need to do a programmatic search using AdvancedSearch, save the results in a search folder and select that folder. When the user searches again an error is occurred indicating that there is such a search folder already.

    Is there any good workaround for this? I have tried to find the previous search results folder but had no success. Also please note that the user will close and after open the Outlook, so I it's impossible to use the reference of the last search results folder (though it didn't help either, as it doesn't have a Parent and thus can't be deleted).

    One bad workaround is to catch the COM exception (MAPI_E_COLLISION) during Search.Save and create a new folder with (1), (2), etc. but this can't be infinite.

    Now I use Outlook 2007 but I have to test it on 2010 as well.

    Thanks in advance.

    Tigran

    Thursday, January 26, 2012 7:08 PM

Answers

  • Have you looked at the Store.GetSearchFolders() method, which returns a collection of Folders that you can search for the name you plan to use?
     
    An alternative is to store the folder EntryID for each search folder you create and store that list somewhere. Then you can read the list and get each search folder using GetFolderFromID().

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Tigran Barseghyan" <=?utf-8?B?VGlncmFuIEJhcnNlZ2h5YW4=?=> wrote in message news:4ee9bb14-0973-4eef-9e1c-41fbf0ab7a3c...

    Hi,

    I am developing Outlook add-in with VSTO. I need to do a programmatic search using AdvancedSearch, save the results in a search folder and select that folder. When the user searches again an error is occurred indicating that there is such a search folder already.

    Is there any good workaround for this? I have tried to find the previous search results folder but had no success. Also please note that the user will close and after open the Outlook, so I it's impossible to use the reference of the last search results folder (though it didn't help either, as it doesn't have a Parent and thus can't be deleted).

    One bad workaround is to catch the COM exception (MAPI_E_COLLISION) during Search.Save and create a new folder with (1), (2), etc. but this can't be infinite.

    Now I use Outlook 2007 but I have to test it on 2010 as well.

    Thanks in advance.

    Tigran


    Ken Slovak MVP - Outlook
    Thursday, January 26, 2012 7:55 PM
    Moderator
  • What if you get the search folder as a Folder object and call its Delete method?
     
    What do you mean by re-use the search folder? You mean the same folder but with a different filter? Once created the filter is read-only to the Outlook object model. It cane be done by using Extended MAPI or Redemption, but it's just as easy to remove an existing folder and add a new one.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Tigran Barseghyan" <=?utf-8?B?VGlncmFuIEJhcnNlZ2h5YW4=?=> wrote in message news:16268e93-cdde-4993-bab8-a7633ab5ea62...

    Ken, thank you for the answer. Now I can get the needed folder.

    But when I try to remove it (Folders.Remove(index)) the folder is not removed as usual. Instead it becomes "inactive" with a name shown in italic and when I click on it a message appears:

    "Cannot display the folder. Microsoft Office Outlook cannot access the specified folder location. The operation cannot be performed because the object has been deleted."

    Even the Count of Folders is not decreased. Any ideas?

    Thanks in advance.

    Tigran

    P.S. Is there a way to reuse the search folder?

    Ken Slovak MVP - Outlook
    Friday, January 27, 2012 6:18 PM
    Moderator

All replies

  • Have you looked at the Store.GetSearchFolders() method, which returns a collection of Folders that you can search for the name you plan to use?
     
    An alternative is to store the folder EntryID for each search folder you create and store that list somewhere. Then you can read the list and get each search folder using GetFolderFromID().

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Tigran Barseghyan" <=?utf-8?B?VGlncmFuIEJhcnNlZ2h5YW4=?=> wrote in message news:4ee9bb14-0973-4eef-9e1c-41fbf0ab7a3c...

    Hi,

    I am developing Outlook add-in with VSTO. I need to do a programmatic search using AdvancedSearch, save the results in a search folder and select that folder. When the user searches again an error is occurred indicating that there is such a search folder already.

    Is there any good workaround for this? I have tried to find the previous search results folder but had no success. Also please note that the user will close and after open the Outlook, so I it's impossible to use the reference of the last search results folder (though it didn't help either, as it doesn't have a Parent and thus can't be deleted).

    One bad workaround is to catch the COM exception (MAPI_E_COLLISION) during Search.Save and create a new folder with (1), (2), etc. but this can't be infinite.

    Now I use Outlook 2007 but I have to test it on 2010 as well.

    Thanks in advance.

    Tigran


    Ken Slovak MVP - Outlook
    Thursday, January 26, 2012 7:55 PM
    Moderator
  • Ken, thank you for the answer. Now I can get the needed folder.

    But when I try to remove it (Folders.Remove(index)) the folder is not removed as usual. Instead it becomes "inactive" with a name shown in italic and when I click on it a message appears:

    "Cannot display the folder. Microsoft Office Outlook cannot access the specified folder location. The operation cannot be performed because the object has been deleted."

    Even the Count of Folders is not decreased. Any ideas?

    Thanks in advance.

    Tigran

    P.S. Is there a way to reuse the search folder?
    Friday, January 27, 2012 5:54 PM
  • What if you get the search folder as a Folder object and call its Delete method?
     
    What do you mean by re-use the search folder? You mean the same folder but with a different filter? Once created the filter is read-only to the Outlook object model. It cane be done by using Extended MAPI or Redemption, but it's just as easy to remove an existing folder and add a new one.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Tigran Barseghyan" <=?utf-8?B?VGlncmFuIEJhcnNlZ2h5YW4=?=> wrote in message news:16268e93-cdde-4993-bab8-a7633ab5ea62...

    Ken, thank you for the answer. Now I can get the needed folder.

    But when I try to remove it (Folders.Remove(index)) the folder is not removed as usual. Instead it becomes "inactive" with a name shown in italic and when I click on it a message appears:

    "Cannot display the folder. Microsoft Office Outlook cannot access the specified folder location. The operation cannot be performed because the object has been deleted."

    Even the Count of Folders is not decreased. Any ideas?

    Thanks in advance.

    Tigran

    P.S. Is there a way to reuse the search folder?

    Ken Slovak MVP - Outlook
    Friday, January 27, 2012 6:18 PM
    Moderator
  • Thank you! It worked.

    Only one thing: when I tried to use Search.Save() immediately after Folder.Delete() it didn't work, as the folder wasn't actually deleted yet. I used Folders.FolderRemove event and now it works.

    Tigran

    Friday, January 27, 2012 6:45 PM