Programming MicrosoftВ® OutlookВ® and Microsoft Exchange 2003, Third Edition (Pro-Developer)

Other Common Outlook Development Tasks

After you start to create applications with Outlook, you might think of development tasks you want to accomplish that are beyond the standard Outlook object library in Outlook forms. We'll look next at three common development tasks in Outlook: automating Outlook from other applications, using CDO in Outlook applications, and programming the Rules Wizard.

Automating Outlook from Other Applications

Outlook supports automation, so you can access Outlook objects from other applications. To access Outlook objects, you typically set a reference to the Outlook object library. For example, to add a reference to the Outlook object library in Visual Basic, you choose References from the Tools menu. In the References dialog box, select the Microsoft Outlook 11.0 Object Library option. The following code sample shows how to use Visual Basic to automate Outlook to return the first Calendar appointment and display it. Notice that the Outlook constant olFolderCalendar can be used in Visual Basic and that replacing it with the actual value is not necessary, as it is in VBScript.

Private Sub Command1_Click() Set oOutlook = CreateObject("Outlook.Application") Set oNS = oOutlook.GetNameSpace("MAPI") Set oCalendar = oNS.GetDefaultFolder(olFolderCalendar) Set oItems = oCalendar.Items Set oFirst = oItems.GetFirst() oFirst.Display End Sub

Using CDO in Outlook

Outlook provides an extensive object library with which you can develop custom applications, but sometimes you'll need to extend this environment by using other object libraries. The object library most commonly used to extend Outlook applications is CDO. CDO provides some functionality for dealing with data stored in Exchange Server that's beyond what is provided by the Outlook object library.

You'll need this additional functionality in the Account Tracking application, discussed in Chapter 8. One requirement for the application is that it keep track of the internal team assigned to work with a particular account. Keeping track of the team includes capturing the team's directory and e-mail information so other internal users who have questions about the account can send the team members e-mail. The easiest way for users to pick account team members is to display the address book. Outlook does not support displaying the address book and returning the individual that the user selected, but CDO does. To take advantage of the CDO functionality, we can extend the Account Tracking application to call the specific CDO functions, as shown here:

Sub FindAddress(FieldName, strCaption, iButtonText) On Error Resume Next Set oCDOSession = application.CreateObject("MAPI.Session") oCDOSession.Logon "","", False, False, 0 txtCaption = strCaption If Not Err Then Set oRecip = oCDOSession.AddressBook (Nothing, txtCaption, _ True, True, 1, iButtonText, "", "", 0) End If If Not Err Then item.UserProperties.Find(FieldName).value = oRecip(1).Name end if oCDOSession.Logoff oCDOSession = Nothing End Sub

As you can see, to take advantage of CDO, you use the CreateObject method of the Application object, passing in the ProgID of CDO, which is MAPI.Session. Next you must log on to a session. Because Outlook already has an active session, the parameters passed to the CDO Logon method force CDO to use the already established Outlook session. From there, the application uses the CDO AddressBook method to bring up the address book with a specific caption and buttons , which enables the user of the application to select a person from the address book. The application then uses the Outlook object library to place the selection of the user in a custom Outlook property. The final task the application performs is to call the Logoff method of CDO and set the CDO object reference to Nothing . These two steps are important because you do not want stray objects left around after your application ends.

The integration of CDO in your Outlook applications does not stop there ”you can also leverage the Outlook library in your CDO applications by using a similar technique. For more information on using the features of CDO in your Outlook application, see Chapter 11.

Installing CDO on Your Computer

Outlook can install CDO on a client computer if the user has not installed it. The following code automates the Windows Installer technology to load CDO on the local machine if CDO is not already installed:

Const msiInstallStateAbsent = 2 Const msiInstallStateLocal = 3 If Not IsCDOInstalled Then ans = Msgbox("CDO is not installed. Would you " & _ "like to install it?", vbYesNo) If ans = vbYes Then InstallCDO(msiInstallStateLocal) Else Ans = Msgbox "CDO is already installed. Would you " & _ "like to remove it?", vbYesNo) If ans = vbYes Then InstallCDO(msiInstallStateAbsent) End If Function IsCDOInstalled() Dim testCDOobj On Error Resume Next Set testCDOobj = CreateObject("MAPI.Session") If Err.Number <> 0 Then IsCDOInstalled = False Else IsCDOInstalled = True End If If Not testCDOobj Is Nothing Then Set testCDOobj = Nothing End If Exit Function End Function Function InstallCDO(iInstallState) Dim blnSuccess Dim objInstaller, OL Dim strProductId, strFeatureName On Error Resume Next Set objInstaller = CreateObject("WindowsInstaller.Installer") Set OL = CreateObject("Outlook.Application") strProductId = OL.ProductCode strFeatureName = "OutlookCDO" If objInstaller.FeatureState(strProductId, strFeatureName) <> _ iInstallState Then objInstaller.ConfigureFeature strProductId, strFeatureName, _ iInstallState If Err.Number <> 0 Then blnSuccess = False Else blnSuccess = True End If Else blnSuccess = True End If Install = blnSuccess End Function

Coding Rules in the Rules Wizard

In Outlook 2002 and later, you can call custom code as an action from the Rules Wizard. This feature allows you to perform more complex parsing of the items that meet the criteria you set in the Rules Wizard. The easiest way to use this feature is to write the code using VBA in Outlook.

Your VBA code must have two subroutines ”one that accepts MailItem objects, and one that accepts MeetingItem objects ”because Outlook will call one of these subroutines, depending on the type of item received. Remember that the Rules Wizard works only on your Inbox, so you cannot script for AppointmentItem objects. The following code shows how to use this new capability. Notice the subroutine names and parameters in the code. The subroutine name and arguments for your custom rule must be exactly the same as those shown.

Sub CustomMailMessageRule(Item As Outlook.MailItem) MsgBox "Mail message arrived: " & Item.Subject End Sub Sub CustomMeetingRequestRule(Item As Outlook.MeetingItem) MsgBox "Meeting request arrived: " & Item.Subject End Sub

Категории