none
Sort emails by size in folder RRS feed

  • Question

  • I have outlook add-in, written in C#. The problem I am facing is I cannot sort emails by size within a folder. Without add-in installed Outlook's Sorting does work (another account is configured ). Now I want to enquire that should I implement a sorting algo in code of add-in, so the mails in folders will be sorted or Outlook has support to sort emails for my add-in in the given folder
    Friday, March 21, 2014 11:01 AM

Answers

  • Hello Ali,

    The Outlook object model provides the Sort method of the Items class. The method sorts the collection of items by the specified property. But the Sort only affects the order of items in a collection. It does not affect the order of items in an explorer view. Please refer to the Sorting Items in a Folder page in MSDN for more information about this.

    To sort items in the view (how Outlook display the items list) you need to customize the current folder view instead. The CurrentView property of the Folder and Explorer class gives you access to a View object representing the current view. Here is what MSDN states for views:

    To obtain a View object for the view of the current Explorer, use Explorer.CurrentView instead of the CurrentView property of the current Folder object returned by Explorer.CurrentFolder.  You must save a reference to the View object returned by CurrentView before you proceed to use it for any purpose. To properly reset the current view, you must do a View.Reset and then a View.Apply. The code sample below illustrates the order of the calls:

    Sub ResetView()  
     Dim v as Outlook.View  
     ' Save a reference to the current view object  
     Set v = Application.ActiveExplorer.CurrentView 
     ' Reset and then apply the current view  
     v.Reset  
     v.Apply  
    End Sub 
    

    You can read more about customizing Views in the Sorting Fields in a View and Filtering and Specifying Fields in a View articles in MSDN.

    • Marked as answer by Marvin_Guo Monday, March 31, 2014 12:00 PM
    Friday, March 21, 2014 11:35 AM

All replies

  • Hello Ali,

    The Outlook object model provides the Sort method of the Items class. The method sorts the collection of items by the specified property. But the Sort only affects the order of items in a collection. It does not affect the order of items in an explorer view. Please refer to the Sorting Items in a Folder page in MSDN for more information about this.

    To sort items in the view (how Outlook display the items list) you need to customize the current folder view instead. The CurrentView property of the Folder and Explorer class gives you access to a View object representing the current view. Here is what MSDN states for views:

    To obtain a View object for the view of the current Explorer, use Explorer.CurrentView instead of the CurrentView property of the current Folder object returned by Explorer.CurrentFolder.  You must save a reference to the View object returned by CurrentView before you proceed to use it for any purpose. To properly reset the current view, you must do a View.Reset and then a View.Apply. The code sample below illustrates the order of the calls:

    Sub ResetView()  
     Dim v as Outlook.View  
     ' Save a reference to the current view object  
     Set v = Application.ActiveExplorer.CurrentView 
     ' Reset and then apply the current view  
     v.Reset  
     v.Apply  
    End Sub 
    

    You can read more about customizing Views in the Sorting Fields in a View and Filtering and Specifying Fields in a View articles in MSDN.

    • Marked as answer by Marvin_Guo Monday, March 31, 2014 12:00 PM
    Friday, March 21, 2014 11:35 AM
  • What exactly do you mean by "I cannot sort emails by size "? Do you mean manually in the folder view? Or programmatically when calling Itens.Sort? Do you get an error?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, March 21, 2014 1:21 PM
  • I mean when I select folder (all email items in it are displayed),  this folder contains emails fetched by Add-in, from our Server and when I try to sort them, they are not sorted by size. 
    What I want to do is to Sort emails by size in View only, how can i achieve this?

    Monday, March 24, 2014 5:25 AM
  • Hello Ali,

    As I already wrote, you need to customize the  CurrentView in Outlook. Please refer to my previous message.

    Monday, March 24, 2014 6:43 AM
  • I am using this code, its throwing exception
    Outlook.View TestView = null;
     TestView = (Outlook.View)(m_oNS.Application.ActiveExplorer().CurrentView);
      ((Outlook.Explorer)explorer).CurrentFolder.Items.Sort("Name");
        TestView.Save();
    Monday, March 24, 2014 7:04 AM
  • Did you try to debug?

    What property or method call exactly fires the exception?

    I'd recommend breaking the chain of call into separate lines of code.

    Monday, March 24, 2014 7:24 AM
  • Hello Sir, 
    I am a bit confused, your code Reset the Current View 

    v.Reset  
     v.Apply

    But i want the content(emails) of Current Folder to be sorted by Size, how is it possible?
    I have used this
    ((Outlook.Explorer)explorer).CurrentFolder.Items.Sort("Size");
    But it doesnot seem to be working

    Best Regards



    • Edited by AliKazmi Monday, March 24, 2014 9:49 AM
    Monday, March 24, 2014 9:47 AM
  • Ali,

    Please take a look at my first message above. It states the following:

    The Outlook object model provides the Sort method of the Items class. The method sorts the collection of items by the specified property. But the Sort only affects the order of items in a collection. It does not affect the order of items in an explorer view. Please refer to the Sorting Items in a Folder page in MSDN for more information about this.

    The Sort method doesn't sort items in the view. You need to customize the CurrentView instead.

    Finally, to properly reset the current view, you must do a View.Reset and then a View.Apply.


    Monday, March 24, 2014 10:32 AM