views:

794

answers:

1

I rarely use VBA because I hate it. Granted, it's hate from lack of understanding; still it's hate all the same. I wrote code below that is very similar to the accepted answer here. It simply marks emails and meeting responses as read and moves them to the archive. After making a button and key assignment, it has worked very well.

The problem occurs after use. I sort my mail by newest on top. After using the macro, the selection defaults to the next email down (the older one). I want it to move to the next email up (the newer one). If I reverse the sorting order of emails, then it works how I want it to work. I've dedicated too much time into this to just reverse the sorting order of my emails. I'll start eating well, getting enough sleep, and exercising before I just reverse my sorting order.

This, I thought, was a minor issue that would be trivial to correct. I tried at first to set a MailItem to Application.ActiveExplorer.CurrentFolder.Items.GetNext then use MailItem.Display. This does a few things wrong (opens rather than changes selection, doesn't know current selection, can't figure out what's considered "next") and I've abandoned the whole path. I then tried setting the Application.ActiveExplorer.Selection.Item property, but I would like to forget that period of my life. I've been through internet Hell (MSDN and Expert*sexchange*) looking for a solution -- no success.

I have a theory that the solution to my problem is too simple to be on the internet. This is intuitive knowledge to everyone else. I, however, am genetically flawed and did not receive this knowledge instinctively.

Sub MoveToArchive()
    On Error Resume Next
    Dim objFolder As Outlook.MAPIFolder
    Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem
    Dim objMRItem As Outlook.MeetingItem
    Set objNS = Application.GetNamespace("MAPI")
    Set objFolder = objNS.Folders("Archive").Folders("Inbox")

    If Application.ActiveExplorer.Selection.Count = 0 Then
        Exit Sub
    End If

    For Each objItem In Application.ActiveExplorer.Selection
        If objFolder.DefaultItemType = olMailItem Then
            If objItem.Class = olMail Then
                objItem.UnRead = False
                objItem.Move objFolder
            End If
        End If
    Next

    For Each objMRItem In Application.ActiveExplorer.Selection
        If objFolder.DefaultItemType = olMailItem Then
            If objItem.Class = olMeetingResponsePositive Or olMeetingResponseNegative Or olMeetingResponseTentative Then
                objMRItem.UnRead = False
                objMRItem.Move objFolder
            End If
        End If
    Next

    Set objItem = Nothing
    Set objMRItem = Nothing
    Set objFolder = Nothing
    Set objNS = Nothing
End Sub
+1  A: 

Outlook provides no way to programmatically select a particular item in the Explorer window. So you would be able to do it this way. The only way I think that you could do it would be to programmatically press the previous key on the tool bar or menu. or redesgin the app to archive in the inspector etc.

76mel
Can you expand on your thought of archiving in the inspector? Thanks
K Richard
you could add an archive button to the toolbar or ribbon. you can then close the current item and open the next or previous item.in the explorer view you could add an arcive folder or archive multipul items and then use the current selection. also you could use context menus in the explorer view.
76mel