VBA for Outlook

Programming in Outlook

Introduction to Outlook and Outlook Events

Mail
Outlook can function as an email client connecting to the Microsoft Exchange server using a proprietary protocol, as well as to standard mail servers using POP3 or IMAP protocol. It provides a multipane view for easy browsing of your mailboxes. You can use Word to compose email. Moreover, it is bundled with an effective client-side junk mail filtering tool.

 

It has other components like Calender, Contacts, Tasks, Journal, Notes.

for additional information, please refer to the given below link: Link Address.  This link has a pdf file which can be downloaded.

 

Outlook code written with Outlook VBA is stored in .VbaProject.OTM File.

Outlook Code window is as good as VBA for Excel Code window.  Features are similar to that of Excel VBA or Access VBA (as shown below - Source: Google Images)

gallery/outlook code window

Most of the code revolves around outlook mails.  Outlook also follows Event Driven Programming Model.  Event is any activity, which asks the application to do a specific job.  There are many types of events, of which, we basically consider Application Events, System Events and User Events.  System events are those that are executed when system asks your application for a specific activity.  User events are those user actions to which the application responds.  The examples of User Events are Click, Right Click, Tab button, Cursor in, Cursor out of text box, user click on drop down list box or also called combo box and many such events follow.  Application events are those that are executed by the application (Outlook).  Examples are: NewMail Event - which is executed when a new mail is received by Outlook Application., MailSent Event is another event, which is executed when a user sends mail. 

gallery/new mail - code

The following code will monitor each mail and display appropriate message as given in the code like: Sender mail address, Mail sent date, Mail received time, Mail Subject, Mail size, and the body of the message... all these in a single message box.  The following Code is to be written in the ThisOutlookSession, as shown in the above picture.

The following are some of the events at item level.  some of them are cancellable events where as the others are not.  Each event has a specefic and designed purpose and usage.  Like wise, we have events for controls, fields and others.  The following link has detailed explanation for each event that exists in Outlook

 

The following link from Microsoft, can provide you with the list of events at each level:

Link to Microsoft Website


Event Name at Item Level

  1. AttachmentAdd
  2. AttachmentRead
  3. AttachmentRemove
  4. BeforeAttachmentAdd
  5. BeforeAttachmentPreview
  6. BeforeAttachmentRead
  7. BeforeAttachmentSave
  8. BeforeAttachmentWriteToTempFile
  9. BeforeAutoSave
  10. BeforeCheckNames
  11. BeforeDelete
  12. Close
  13. CustomAction
  14. CustomPropertyChange
  15. Forward
  16. Open
  17. PropertyChange
  18. Read
  19. Reply
  20. ReplyAll
  21. Send
  22. Unload
  23. Write

Not all the events we use in programming, but on case-to-case basis, depending on the requirement and situation.

 

We can connect outlook to Ms.Access database as well.