none
Loop thru messages in an date time order RRS feed

  • Question

  • Hi Guys,

    I would like to scan thru the outlook messages in a folder, but sort them descending by date/time only for the use in the loop is that possible ?

    This is the script i use to loop thru them now :

        Public olApp As Outlook.Application = New Outlook.Application
        Public olNamespace As Outlook.NameSpace = olApp.GetNamespace("MAPI")
        Public olFolders As Object = olNamespace.Folders
        Public readCollection As Microsoft.Office.Interop.Outlook.Items
        Public olReadFolder As Outlook.Folder
        Public olReadFolder1 As Outlook.Folder
    
    public sub scanMsg()
    
    olReadFolder = olNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)     
            olReadFolder1 = olReadFolder.Folders("foldername")
    
            Dim currentMessage As Outlook.MailItem
    
            For Each currentMessage In olReadFolder1.Items
               
              'loop'
    
            Next
    
    end sub

    Wednesday, November 21, 2012 3:03 PM

Answers

  • You can sort an Items collection by date if desired and the sort would be only for that instance of the Items collection. See the Object Browser Help for Items.Sort, either in the Outlook VBA project or wherever your code is running.
     
    Where is the code running? If it's in a COM addin or the Outlook VBA project do not create a new instance of the Outlook Application object, use the one passed to you.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Jacob_Siemaszko" <=?utf-8?B?SmFjb2JfU2llbWFzemtv?=> wrote in message news:6626b62f-94f5-4e0f-896b-407d8e294e0b...

    Hi Guys,

    I would like to scan thru the outlook messages in a folder, but sort them descending by date/time only for the use in the loop is that possible ?

    This is the script i use to loop thru them now :

        Public olApp As Outlook.Application = New Outlook.Application
        Public olNamespace As Outlook.NameSpace = olApp.GetNamespace("MAPI")
        Public olFolders As Object = olNamespace.Folders
        Public readCollection As Microsoft.Office.Interop.Outlook.Items
        Public olReadFolder As Outlook.Folder
        Public olReadFolder1 As Outlook.Folder
    
    public sub scanMsg()
    
    olReadFolder = olNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)     
            olReadFolder1 = olReadFolder.Folders("foldername")
    
            Dim currentMessage As Outlook.MailItem
    
            For Each currentMessage In olReadFolder1.Items
               
              'loop'
    
            Next
    
    end sub


    Ken Slovak MVP - Outlook
    Wednesday, November 21, 2012 3:11 PM
    Moderator
  • Do not create a new Outlook.Application instance. That's not a good thing to do at all in an Outlook COM addin. In your addin startup (either OnConnection for a shared addin or the VSTO startup handler) assign your Application object from the trusted Application passed to you.
     
    In a shared addin you'd use "application" that's passed in OnConnection(). In VSTO you'd use this.Application (or for VB.NET use Me.Application) and assign that to a class level Outlook.Application object.
     
    Declare an explicit Items collection object and assign that, don't use something like this compound dot operator construction: foldervariable.items.
     
    Sort("[ReceivedTime]", True
     
    Make sure you get the property names correct, it won't work if they are incorrect.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Jacob_Siemaszko" <=?utf-8?B?SmFjb2JfU2llbWFzemtv?=> wrote in message news:faed7b4f-4009-4d38-8ce9-a47780dc07c0...

    Thanks, i found it, but i still do not understeand how to use it.

    I want to order descending on receivetime do i use it like this ?

    foldervariable.items.sort("[receivetime]",true)   ???

    its an outlook add in im creating with visual studio 2010



    Ken Slovak MVP - Outlook
    Wednesday, November 21, 2012 7:56 PM
    Moderator
  • I have the answer

    Dim readCollection As Micrsoft.Office.Interop.Outlook.Items Dim olReadFolder As Outlook.Folder Dim olReadFolder1 As Outlook.Folder olReadFolder = olNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)

    olReadFolder1 = olReadFolder.Folders("schedTasks") readCollection = olReadFolder1.Items readCollection.Sort("[ReceivedTime]", False) Dim currentMessage As Outlook.MailItem For Each currentMessage In readCollection msgbox (currentmessage.receivedtime) Next


    This works :) Thanks for the input !!!
    Friday, November 23, 2012 1:19 PM

All replies

  • You can sort an Items collection by date if desired and the sort would be only for that instance of the Items collection. See the Object Browser Help for Items.Sort, either in the Outlook VBA project or wherever your code is running.
     
    Where is the code running? If it's in a COM addin or the Outlook VBA project do not create a new instance of the Outlook Application object, use the one passed to you.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Jacob_Siemaszko" <=?utf-8?B?SmFjb2JfU2llbWFzemtv?=> wrote in message news:6626b62f-94f5-4e0f-896b-407d8e294e0b...

    Hi Guys,

    I would like to scan thru the outlook messages in a folder, but sort them descending by date/time only for the use in the loop is that possible ?

    This is the script i use to loop thru them now :

        Public olApp As Outlook.Application = New Outlook.Application
        Public olNamespace As Outlook.NameSpace = olApp.GetNamespace("MAPI")
        Public olFolders As Object = olNamespace.Folders
        Public readCollection As Microsoft.Office.Interop.Outlook.Items
        Public olReadFolder As Outlook.Folder
        Public olReadFolder1 As Outlook.Folder
    
    public sub scanMsg()
    
    olReadFolder = olNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)     
            olReadFolder1 = olReadFolder.Folders("foldername")
    
            Dim currentMessage As Outlook.MailItem
    
            For Each currentMessage In olReadFolder1.Items
               
              'loop'
    
            Next
    
    end sub


    Ken Slovak MVP - Outlook
    Wednesday, November 21, 2012 3:11 PM
    Moderator
  • Thanks, i found it, but i still do not understeand how to use it.

    I want to order descending on receivetime do i use it like this ?

    foldervariable.items.sort("[receivetime]",true)   ???

    its an outlook add in im creating with visual studio 2010


    Wednesday, November 21, 2012 4:07 PM
  • Do not create a new Outlook.Application instance. That's not a good thing to do at all in an Outlook COM addin. In your addin startup (either OnConnection for a shared addin or the VSTO startup handler) assign your Application object from the trusted Application passed to you.
     
    In a shared addin you'd use "application" that's passed in OnConnection(). In VSTO you'd use this.Application (or for VB.NET use Me.Application) and assign that to a class level Outlook.Application object.
     
    Declare an explicit Items collection object and assign that, don't use something like this compound dot operator construction: foldervariable.items.
     
    Sort("[ReceivedTime]", True
     
    Make sure you get the property names correct, it won't work if they are incorrect.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Jacob_Siemaszko" <=?utf-8?B?SmFjb2JfU2llbWFzemtv?=> wrote in message news:faed7b4f-4009-4d38-8ce9-a47780dc07c0...

    Thanks, i found it, but i still do not understeand how to use it.

    I want to order descending on receivetime do i use it like this ?

    foldervariable.items.sort("[receivetime]",true)   ???

    its an outlook add in im creating with visual studio 2010



    Ken Slovak MVP - Outlook
    Wednesday, November 21, 2012 7:56 PM
    Moderator
  • Hi ,

    I understeand your point about the bad approach to this add in, and I will concentrate on how to organize it in a better way. But for now i just would like this function to sort, and i cannot get it work like this, is it normal or should it work ?

    olReadFolder1.Items.Sort("[ReceivedTime]", True) Dim currentMessage As Outlook.MailItem For Each currentMessage In olReadFolder1.Items MsgBox(currentMessage.ReceivedTime) Exit Sub Next

    Friday, November 23, 2012 12:17 PM
  • I have the answer

    Dim readCollection As Micrsoft.Office.Interop.Outlook.Items Dim olReadFolder As Outlook.Folder Dim olReadFolder1 As Outlook.Folder olReadFolder = olNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)

    olReadFolder1 = olReadFolder.Folders("schedTasks") readCollection = olReadFolder1.Items readCollection.Sort("[ReceivedTime]", False) Dim currentMessage As Outlook.MailItem For Each currentMessage In readCollection msgbox (currentmessage.receivedtime) Next


    This works :) Thanks for the input !!!
    Friday, November 23, 2012 1:19 PM