Making Microsoft Outlook Useful - Part 1

Tuesday Nov 19th 2002 by Sam Huggill
Share:

Making Microsoft Outlook Useful

In the days of multiple mail boxes, huge amounts of email and small amounts of dosh, us humble programmers must sometimes resort to performing a little bit of VB to solve a problem rather then splashing out on some new, untested software.

Throughout this article youll learn how to access some of the features of Microsoft Outlook from Visual Basic, and make the very good Outlook object model work for you.

So, lets begin!

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 thats 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 youre running).

To start with, were 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 objFolderEnd 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 well 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 objMailEnd Sub

(Note: If youve 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 weve read folders and messages, lets take a look at performing some basic messaging tasks...

So you think youre ready to play around with messages eh? Well, true, you probably are. Messages are referred to as items in Outlook, as you can also have appointments & notes etc. For now, well be dealing with the items located within the Inbox folder.

The next snippet of code shows you how to simple create a message, write a short message, and save it in the Drafts folder:

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)    Set objMail = objInbox.Items.Add  With objMail    .Subject = "Well done"    .Body = "This article is great!"    .To = "sam@vbsquare.com"    .Save  End WithEnd Sub

If you look closely at the code youll see that Ive been rather cheeky with the body and subject lines, so to get your own back you can run the next piece of code to delete my ego booster!

Private Sub Form_Load()  Dim objOutlook As New Outlook.Application  Dim objNameSpace As Outlook.NameSpace  Dim objDrafts As MAPIFolder  Dim objMail As MailItem      'Get the MAPI reference  Set objNameSpace = objOutlook.GetNamespace("MAPI")    'Pick up the Drafts folder  Set objDrafts = objNameSpace.GetDefaultFolder(olFolderDrafts)    Set objMail = objDrafts.Items("Well done")  objMail.DeleteEnd Sub

Right, thats just about it for the first part in this series! In part 2 youll learn how to:

  • Filter emails as you read them in through different criteria
  • Build a program which allows you to:
  • Read in messages from a folder
  • Filter duplicates & blanks
  • Apply a date criteria
  • Locate the email details
  • Write them to a file

See you again soon for some more exciting Outlook programming!

Sam

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved