?
Solved

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

Posted on 2003-10-23
3
Medium Priority
?
1,577 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 300 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses

770 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