Making Microsoft Outlook Useful - Part 2
Right, lets get our heads down to some serious code! Reading through messages in a given folder was introduced to you in the previous article, so you should be up to speed on that. Now the first thing Im going to show you is an easy way to get only the latest messages, within a few days of the current date:
(For this code you need a list box called lstNewMessages and a text box called txtDate with a .text value of something like 01/01/01)
Private Sub Form_Load() Dim objOutlook As New Outlook.Application Dim objNameSpace As Outlook.NameSpace Dim objFolder As MAPIFolder Dim objMail As MailItem ' Get the MAPI name space Set objNameSpace = objOutlook.GetNamespace("MAPI") ' Get a ref to the folder we want Set objFolder = objNameSpace.GetDefaultFolder(olFolderInbox) ' Read through all the items For i = 1 To objFolder.Items.Count Set objMail = objFolder.Items(i) ' Check the sent date for validity If objMail.SentOn >= CDate(txtDate.Text) Then ' Add it to the list box lstNewMessages.AddItem objMail.Subject lstNewMessages.ItemData(lstNewMessages.NewIndex) = i End If Next iEnd Sub
OK. This is a pretty basic start. Lets move on to something a bit more demanding...
Page 3 of 4