Solved

Sort method (VBA Outlook). Easy question (I think)

Posted on 2003-10-23
3
1,309 Views
Last Modified: 2012-05-04
Hi,

I'm trying to create a button to send to the clipboard the dates and subjects of all the emails in the current folder but I can't get them sorted by subject! I'm using the 'Sort' method but doesn't seem to work at all (doesn't generate an error but doesn't sort my items neither).

Any idea of what I'm doing wrong? Is there any other way to get the same? Here's the code if you wanna take a look. THANKS!


    Dim app As Outlook.Application
    Dim nameSpace As nameSpace
    Dim currentFolder As MAPIFolder
    Dim st As String
     
    Set app = CreateObject("Outlook.Application")
    Set nameSpace = app.GetNamespace("MAPI")
    Set currentFolder = app.ActiveExplorer.currentFolder
    Set myItems = app.ActiveExplorer.currentFolder.Items

    ' currentFolder.Items.IncludeRecurrences = True

    myItems = myItems.Sort("[Subject]")
   
    For k = 1 To currentFolder.Items.Count
   
        st = st & "Sent: " & currentFolder.Items(k).SentOn & _
             " Subject: " & currentFolder.Items(k).Subject & " " & vbNewLine
    Next
 
    ClipBoard_SetData (st)
0
Comment
Question by:pablo_picasso
  • 2
3 Comments
 
LVL 11

Expert Comment

by:Steiner
ID: 9607223
I'd guess that .Sort only affects the DISPLAY of the folder, but not the items themselves. Therefore looping through the items you won't see a difference, because their "index" is still the same as before the sort.
I guess you'll have to get your strings first and then sort THESE instead of the items.
0
 
LVL 11

Accepted Solution

by:
Steiner earned 100 total points
ID: 9607274
Sorry, I was wrong (my OL-VBA-Help does not work here, so it was a guess as I said). But searching further, I found the following page stating that the example is wrong and you should use another example:

http://support.microsoft.com/support/kb/articles/Q183/0/53.asp
WRONG:
   Sub SortContacts()
      Set ol = New Outlook.Application
      Set olns = ol.GetNameSpace("MAPI")
      Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
      MyFolder.Items.Sort "[CompanyName]", False
      ' MyFolder.Items causes Outlook to re-fetch the collection.
      For Each MyItem in MyFolder.Items
         MsgBox MyItem.CompanyName
      Next MyItem
   End Sub

RIGHT:
   Sub SortContacts()
      Set ol = New Outlook.Application
      Set olns = ol.GetNameSpace("MAPI")
      Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
      ' Create a collection of items
      Set MyItems = MyFolder.Items
      ' Sort the collection
      MyItems.Sort "[CompanyName]", False
      '  Loop through the sorted collection
      For Each MyItem in MyItems
         MsgBox MyItem.CompanyName
      Next MyItem
   End Sub

So it seems using Folder.Items.Sort is the problem. Get the items first (MyItems) and then sort them.
0
 

Author Comment

by:pablo_picasso
ID: 9613028
Thanks Steiner!!!!!!!!!!!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now