Sort mailitem programatically by ReceivedTime and get the details of selected mailitem?

Answered Sort mailitem programatically by ReceivedTime and get the details of selected mailitem?

  • lunes, 04 de octubre de 2010 10:08
     
     

    Hi,

     

    I have created a menuitem in Outlook 2007 and I want my mailitems to get sorted on that click event, but I am unable to do that.  Also after sorting, I should be able to get the required details for the selected mailitem, i.e., which mailitem was selected in the order and then later its details. Can anyone please help with this?

    My code goes like this:

    myCustomButton is the button I created using code.

    Public Sub myCustomButton_Click(ByVal buttonControl As Office.CommandBarButton, ByRef Cancel As Boolean)
            Dim olApp As Microsoft.Office.Interop.Outlook.Application
            Try
                olApp = GetObject(, "Outlook.Application")
            Catch ex As System.Exception
                'Err.Number = 429
                olApp = CreateObject("Outlook.Application")
            End Try
            Dim olNameSpace As Microsoft.Office.Interop.Outlook.NameSpace = olApp.GetNamespace("MAPI")
            Dim olFolders As Microsoft.Office.Interop.Outlook.Folders = olNameSpace.Folders
            Dim olUSMBMailboxFolder = olFolders.Item("My Custom Mailbox") 'this is my custom folder - OUTLOOK DATA FILE.
            Dim olUSMBInbox = olUSMBMailboxFolder.Folders.Item("Inbox").Items 'this INBOX is of 'My Custom Mailbox' - Not the default one
            olUSMBInbox.Sort("[ReceivedTime]", False) 'this line is not working.  All the mailitems still remain in default Outlook sort order

            'I need to check if the mailitems are sorted here, and if so, go to the below code for further processing.
            'I also need to know how to check if mailitems are sorted by 'ReceivedTime' here.

            For Each olUSMBInboxMailitem As Object In olUSMBInbox
                Dim testJunkSub As String
                testJunkSub = olUSMBInboxMailitem.Subject.ToString.Substring(0, olUSMBInboxMailitem.Subject.ToString.IndexOf(":") + 1)
                'these are the mailitems I do not want to read, so filtering them.
                If testJunkSub = "Undeliverable:" Or testJunkSub = "Message Recall Success:" Or testJunkSub = "Message Recall Failure:" Or testJunkSub = "Out of Office:" Then
                    'do nothing
                Else
                    'I fetch the required details (SenderName, Subject, Body, etc.) from here and send to database
                    MsgBox(olUSMBInboxMailitem.SenderName.ToString)
                End If
            Next
        End Sub

     

    regards,

     

    Vamshi

Todas las respuestas

  • lunes, 04 de octubre de 2010 15:07
    Moderador
     
     Respondida

    The Items collection is a collection of data that's only available in code.  Calling Items.Sort does not update the list of messages displayed in the folder.  To do that, you need to work with View objects and customize the sort order in an existing or new view within that folder.

    SortFields Property [Outlook 2007 Developer Reference]:
    http://msdn.microsoft.com/en-us/library/bb207459(office.12).aspx

    Also, one thing I noticed in your code is that you are using the GetObject and CreateObject methods to get a reference to the Outlook.Application object.  You should always inherit these objects from the ThisAddin class, otherwise you run the risk of your code not being fully trusted.


    (P.S. Please don't forget to mark this post as an answer if it has helped you)
    Eric Legault: MVP (Outlook), MCTS (SharePoint)
    Owner: Collaborative Innovations
    Twitter: elegault
    Blog: Eric Legault My Eggo
    • Marcado como respuesta vamshivemula1 miércoles, 06 de octubre de 2010 13:18
    •  
  • lunes, 04 de octubre de 2010 22:21
     
     

    Hi Eric,

    Appreciate your efforts for notifying the problematic areas in my code.  This is my first VSTO add-in, and it would be a great deal of help if you can give an example with sample code for working with View Objects and customizing the sort order in a existing view.

    Regards,

    Vamshi


    Vamshi Vemula
  • martes, 05 de octubre de 2010 2:49
    Moderador
     
     

    Here's a sample:

    Sorting Fields in a View:
    http://msdn.microsoft.com/en-us/library/ff862429.aspx


    (P.S. Please don't forget to mark this post as an answer if it has helped you)
    Eric Legault: MVP (Outlook), MCTS (SharePoint)
    Owner: Collaborative Innovations
    Twitter: elegault
    Blog: Eric Legault My Eggo