Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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

618 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