Special Edition Using Microsoft Office Outlook 2003

Now that you know some of the basics about manipulating objects in VBA, you can use VBA to respond to various application-level events in Outlook. There are 10 application-level events you can use in Outlook 2003:

  • AdvancedSearchComplete Triggered when an Advanced Find completes

  • AdvancedSearchStopped Triggered when an Advanced Find is stopped by the user

  • ItemSend Occurs when an item is sent

  • MAPILogonComplete Occurs when a user has logged on to Outlook

  • NewMail Occurs when a new email is received

  • NewMailEx Occurs when a new email is received through the Exchange Server

  • OptionsPagesAdd Occurs when a new page is added to the Options dialog box

  • Quit Triggered when the Outlook application terminates

  • Reminder Occurs when Outlook displays a reminder

  • StartUp Occurs when Outlook loads

You can add code to any of these events to customize your Outlook installation. Outlook provides empty events for you to use to start your coding. To access these events, select ThisOutlookSession from the Project Explorer and press F7 on the keyboard. This opens the code window for the selected item in the Project Explorer.

NOTE

Outlook 2002 and Outlook 2003 include enhanced security features developed in response to the Melissa and I Love You virus attacks. These security features display messages whenever code attempts to access email addresses or tries to send code programmatically. When Outlook 2002 was released, developers found it hard to write code in Outlook VBA that wouldn't trigger the security prompts. Outlook 2003 fixes this problem by automatically trusting the built-in Application object in Outlook 2003 VBA. So, as long as your objects and code in Outlook 2003 VBA use the default Application object (rather than defining your own new Application object), your code is automatically trusted by Outlook 2003.

When you first open the code for ThisOutlookSession, you'll see the general object and the declarations procedure. Use the Object drop-down list to select the Application object. The Procedure drop-down list will probably change to ItemSend. To access other events, use the Procedure drop-down list and choose the event you want to add code to. The following code examples use the ItemSend event.

The default ItemSend event uses the following code:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) End Sub

There are two default variables in the ItemSend event. The first is a variable, Item, that refers to the actual item being sent. The second is a Boolean value, Cancel. Setting Cancel to true prevents the item from actually being sent.

The following code checks every outgoing message for the presence of the phrase Urban Flood Control. If the phrase is found, a copy of the item is added to your Urban Flood Control folder.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Dim olNS As Outlook.NameSpace Dim olfolder As Outlook.MAPIFolder Dim olItem As Outlook.MailItem Dim txtSearchString As String txtSearchString = "Urban Flood Control" Set olNS = Application.GetNamespace("MAPI") Set olfolder = olNS.GetDefaultFolder(olFolderInbox) If InStr(Item.Body, txtSearchString) Then 'Search String Found Set olItem = Item.Copy With olItem .Move (olfolder.Folders("Urban Flood Control")) .Close 0 End With End If Set olItem = Nothing Set olfolder = Nothing Set olNS = Nothing End Sub

The next time you send a message, this code will run. If the search string is found within the message, a copy of the unsent message will be moved to the Urban Flood Control folder. You can add code to all the application-level events in Outlook.

Категории