Other Common Tasks in Outlook Development

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.

Automating Outlook Office Documents

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.

Using VBA with an Outlook Office Document

The following example shows you how to add VBA code to an Outlook Office document based on Excel 97.

  1. In Outlook, on the File menu, point to New and then select Office Document. In the New Office Document dialog box, select Microsoft Excel Worksheet and click OK.
  2. In the displayed Microsoft Outlook dialog box, select either the post or the send option and click OK.
  3. From the Tools menu on the Excel form, point to Forms and then select Design This Form.
  4. From the Tools menu, point to Macro and then select Visual Basic Editor.
  5. Expand the project explorer, which is on the left, until you find the ThisWorkbook object. Double-click on the object to display the code window.
  6. From the Object drop-down list, select Workbook. Excel should automatically place a Workbook_Open subroutine in the code window.
  7. In the procedure, use the MsgBox function to display some text. For example, you could add this:
  8.  Private Sub Workbook_Open() Msgbox "This is from Excel" End Sub 

  9. Close the Visual Basic Editor.
  10. On the Excel form, select Run This Form from the Form menu.
  11. In the displayed message box, click Yes to indicate that you trust the macros in the workbook. After you click Yes, the message box that you added earlier will be displayed.

Using VBScript with an Outlook Office Document

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.

  1. In Outlook, from the File menu, point to New and then select Office Document. Select Microsoft Word Document, and click OK.
  2. In the displayed Microsoft Outlook dialog box, select either the post or the send option and click OK.
  3. From the Tools menu on the Word form, point to Forms and then select Design This Form.
  4. Select View Code from the Form menu.
  5. Type the following lines of code into the Script Editor:
  6.  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 

  7. On the Word form, from the Form menu, select Run This Form. A Word document will be displayed with the contact name already entered.

Automating Outlook from Other Applications

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 

Using CDO in Outlook

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.



Programming Microsoft Outlook and Microsoft Exchange
Programming Microsoft Outlook and Microsoft Exchange, Second Edition (DV-MPS Programming)
ISBN: 0735610193
EAN: 2147483647
Year: 1999
Pages: 101

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net