Solved

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

Posted on 2003-10-23
3
1,415 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

860 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