I've got a macro in Excel that analyze all the mail in outlook and write some parameter in an Excel sheet, it starts from the latest one. How can I start instead from the oldest?
CodePudding user response:
Retrieve the Inbox folder (assuming that is what you want) using Namespace.GetDefaultFolder(olFolderInbox). Retrieve the Items collection from the MAPIFolder.Items property, sort the collection by calling Items.Sort("ReceivedTime", true), then loop through the items using for each.
CodePudding user response:
It seems you just need to sort the items in the required order:
Items.Sort("ReceivedTime", true)
Note that iterating over all items in Outlook is not really a good idea. You may consider using the Find/FindNext or Restrict methods of the Items class instead. So, you could iterate over items that correspond to your conditions. Read more about these methods in the following articles:
- How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
- How To: Use Restrict method to retrieve Outlook mail items from a folder
Also you may find the AdvancedSearch method of the Application class helpful. The key benefits of using the AdvancedSearch method in Outlook are:
- The search is performed in another thread. You don’t need to run another thread manually since the
AdvancedSearchmethod runs it automatically in the background. - Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The
RestrictandFind/FindNextmethods can be applied to a particularItemscollection (see theItemsproperty of theFolderclass in Outlook). - Full support for DASL queries (custom properties can be used for searching too). To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the
IsInstantSearchEnabledproperty of theStoreclass). - You can stop the search process at any moment using the
Stopmethod of theSearchclass.
See Advanced search in Outlook programmatically: C#, VB.NET for more information.
