Change the selected item to next or previous

13,681

Solution 1

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.

Solution 2

Insert into your code one of the two lines of SendKeys code from one of the following two macros to cause Outlook to change the selection in the Outlook Item List pane to the message above or below the active message. The email message item you are moving up or down from must be active in the Item List pane in the Outlook application before the SendKeys code executes. If necessary add a time delay in your code to allow time for the appropriate message to become active in Outlook before the SendKeys code executes. These macros could also be added to the ribbon in Outlook 2010 to create up and down arrow buttons to navigate in the Outlook Item List Pane.

-- Sub MoveUp()

SendKeys "{UP}": DoEvents

End Sub

Sub MoveDown()

SendKeys "{Down}": DoEvents

End Sub

Solution 3

I was able to achieve a similar effect by using the SendKeys function.

I wanted to return to the first message in the explorer, after doing some manipulation of the selected items.

I just added the following line after the manipulation is done:

SendKeys "{HOME}"

This works for me, on this particular case but can be modified to be used in other context.

Share:
13,681
Rishabh
Author by

Rishabh

mostly use ms sql and .net

Updated on June 04, 2022

Comments

  • Rishabh
    Rishabh almost 2 years

    I wrote code below that is very similar to the accepted answer here. It marks emails and meeting responses as read and moves them to the archive.

    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. I've dedicated too much time to this to just reverse the sorting order of my emails.

    I tried to set a MailItem to Application.ActiveExplorer.CurrentFolder.Items.GetNext then use MailItem.Display. This opens rather than changes selection, doesn't know current selection, can't figure out what's considered "next".

    I tried setting the Application.ActiveExplorer.Selection.Item property. I've been through (MSDN and Expertsexchange) looking for a solution.

    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
    
  • Rishabh
    Rishabh almost 15 years
    Can you expand on your thought of archiving in the inspector? Thanks
  • 76mel
    76mel almost 15 years
    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.