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:
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
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. |