Log in

View Full Version : make last message sent active (highlightied) in sent mail folder



framednlv
07-07-2016, 12:38 PM
I need to make the last message sent active or highlighted in the sent mail folder. This is what I have so far:


Private Sub SentSave()
Dim objOL As Object 'As Outlook.Application
Dim objFolder As Object 'As Outlook.Folder
'get current folder
Set objOL = CreateObject("Outlook.Application")
Set objFolder = objOL.ActiveExplorer.CurrentFolder
Set objOL.ActiveExplorer.CurrentFolder = objFolder
Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myOlApp.ActiveExplorer.CurrentFolder = myNamespace.GetDefaultFolder(olFolderSentMail)
Application.ActiveExplorer.ClearSelection
Application.ActiveExplorer.Activate

'<here I need to have the last item sent highlighted in the sentmail folder

'save emails to job folders using listbox
UserForm1.Show
'return to original folder
Set objOL.ActiveExplorer.CurrentFolder = objFolder

End Sub



Any help would be appreciated.
Chris

gmayor
07-07-2016, 10:04 PM
Given that you have posted in an Outlook forum I assume we can expect that the macro is to be run in Outlook VBA, in which case you don't have to create a new Outlook application, you are already in Outlook.

You don't need to select a message in order to process it, you merely have to identify it to the macro. The following will identify the latest item in the sent mail folder.

Your Userform is a mystery so I cannot comment on that.


Private Sub SentSave()
Dim olItems As Items
Dim olItem As Object

Set olItems = Session.GetDefaultFolder(olFolderSentMail).Items
olItems.Sort "[Received]", True

'<here I need to have the last item sent highlighted in the sentmail folder
Set olItem = olItems(1) 'You have it

'save emails to job folders using listbox
UserForm1.Show

End Sub

framednlv
07-10-2016, 07:55 AM
Graham,
Thanks for the code but, with out going into detail, I need it to work as described in the first post.

gmayor
07-10-2016, 08:21 PM
In that case add
Session.GetDefaultFolder(olFolderSentMail).GetExplorer.Displaybefore
Set olItems = Session.GetDefaultFolder(olFolderSentMail).Items to display the folder.