Hopefully you all use folders within your Outlook environment – I must admit that I only recently realised that folders would actually be useful to save my inbox exceeding 100 messages (although it still currently stands at around 500, but that’s not the point!)
Along with folders come rules (Tools -> Rules Wizard), which are nifty little things that shift certain emails to certain folders. Very handy.
Anyway, enough of the Outlook basics; lets delve into some programming!
Open up VB, select a new Standard VB Project. Click Project, References and scroll down and check Microsoft Outlook 9 Object Library (or whatever version of Outlook you’re running).
To start with, we’re just going to make a simple project that loads Outlook and reads the folders present. Add the following code to the form:
Private Sub Form_Load()
Dim objOutlook As New Outlook.Application Dim objNameSpace As Outlook.NameSpace Dim objInbox As MAPIFolder Dim objFolder As MAPIFolder
注释:Get the MAPI reference Set objNameSpace = objOutlook.GetNamespace("MAPI")
注释:Pick up the Inbox Set objInbox = objNameSpace.GetDefaultFolder(olFolderInbox)
注释:Loop through the folders under the Inbox For Each objFolder In objInbox.Folders lstFolders.AddItem objFolder.Name Next objFolder
End Sub
You will also need to add a list box, named lstFolders.
Give that a run and hopefully all the folders under your Inbox will appear in the list box.
OK, that was a pretty simple example of how to loop through all the folders under the Inbox and read them. Now we’ll try reading some messages...
Private Sub Form_Load()
Dim objOutlook As New Outlook.Application Dim objNameSpace As Outlook.NameSpace Dim objInbox As MAPIFolder Dim objMail As MailItem
注释:Get the MAPI reference Set objNameSpace = objOutlook.GetNamespace("MAPI")
注释:Pick up the Inbox Set objInbox = objNameSpace.GetDefaultFolder(olFolderInbox)
注释:Loop through the items in the Inbox For Each objMail In objInbox.Items lstFolders.AddItem objMail.Subject Next objMail
End Sub
(Note: If you’ve got a large number of items in your Inbox like me, you might want to limit the number of messages that the program reads in).
Now that we’ve read folders and messages, lets take a look at performing some basic messaging tasks...