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. This section highlights three common development tasks in Outlook: automating Office documents, automating Outlook from other applications, and using CDO in Outlook applications.
In Outlook, you can use Office documents as the basis for your collaborative applications. For example, you can create an expense report application that uses the calculation features of Excel while giving users the ability to e-mail and categorize expense reports using the features of Outlook.
When using Office documents as forms, you can customize your application in two ways: through VBA in the Office document, or through VBScript in Outlook. Let's examine both of the ways you can automate an Office document application.
The following example shows you how to add VBA code to an Outlook Office document based on Excel 97.
Private Sub Workbook_Open() Msgbox "This is from Excel" End Sub |
There might be times when you would rather automate the Office application embedded in the Outlook Office document than create VBA code in the Office document. The most common example of this automation strategy is to write VBScript code that retrieves information from Outlook sources and places it in the Office document. You would use the GetObject method to get the currently running instance of the Office application. The next set of steps show you how to create an Outlook Office document based on Word 97 and automatically take the name of a contact and place it into the Word document.
NOTE
For this functionality to work properly, you must have the latest version of VBScript installed on your machine. The version that ships with Outlook 98 is VBScript version 3.1. This version has a bug that causes the GetObject function to start a new instance of the Office application rather than use the existing instance in the Outlook Office document form. To download the latest version of VBScript, refer to http://msdn.microsoft.com/scripting.
Sub Item_Open() set oWord = GetObject(,"Word.Application") set oNS = Application.GetNameSpace("MAPI") set oContact = oNS.GetDefaultFolder(10).Items.GetFirst oWord.Selection.TypeText "Dear " & oContact.Subject End Sub |
Since Outlook supports Automation, you can access the Outlook objects from other applications. To access the 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, select References from the Project menu. In the References dialog box, check the Microsoft Outlook 98 Object Model option and click OK. The next code sample shows you 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, and it is not necessary to replace it with the actual value as is required 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 |
As you have seen, Outlook provides an extensive object library with which you can develop custom applications. However, at times 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 beyond that provided by the Outlook object library.
You'll need this additional functionality in the Account Tracking application, which is discussed in Chapter 7. 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 that other internal users who have questions about the account can send 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, the Account Tracking application is extended to call the specific CDO functions, as shown here:
Sub FindAddress(FieldName, Caption, ButtonText) On Error Resume Next Set oCDOSession = application.CreateObject("MAPI.Session") oCDOSession.Logon "", "", False, False, 0 txtCaption = Caption if not err then Set orecip = oCDOSession.addressbook (Nothing, txtCaption, _ True, True, 1, ButtonText, "", "", 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 from the preceding code, to take advantage of CDO, you use the CreateObject method of the Application object. You then pass to this method the ProgID of CDO, which is MAPI.Session. Next CDO requires that you 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 object reference to CDO to Nothing. These two steps are important because you do not want stray objects left around after your application ends.
As you have seen, you can leverage CDO in your Outlook applications. However, the integration does not stop there—you can also leverage the Outlook library in your CDO applications using a similar technique. For more information on the features of CDO and how you can use them in your Outlook application, see Chapter 11.