Solved

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

Posted on 2003-10-23
3
1,451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

740 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